05AB1E, 9 bytes
sUXGD}XGm # (implicit) push STDIN to stack s # swap top two stack values U # store top of stack in X X # push X to stack G # for N in [1, 2, ..., top of stack - 1]... D # duplicate top of stack } # exit loop X # push X to stack G # for N in [1, 2, ..., top of stack - 1]... m # push (second to top of stack) ^ (top of stack) # (implicit) exit loop, output top of stack to STDOUT