N, Q = map(int, input().split())
queries = [tuple(map(int, input().split())) for _ in range(Q)]

A = [i + 1 for i in range(N)]

rev = False

for query in queries:
    match query[0]:
        case 1:
            _, x, y = query
            if rev:
                A[-x] = y
            else:
                A[x - 1] = y
        case 2:
            rev = not rev
        case 3:
            _, x = query
            if rev:
                print(A[-x])
            else:
                print(A[x - 1])
        case _:
            assert False
