Under construction

Still being built. Things may change

Finding the Stop Key

Time limit1sMemory limit1024 MB

Summary
Given a sequence of commands that swap pairs of 8 keys (invalid commands are skipped), find the final point index of the stop key.
Level

Easy2 of 10

Topics
Simulation, Bit manipulation
Solved
No attempts yet

Problem

A factory robot has malfunctioned and is running out of control. To stop it, you must find which of the 8 keys is the stop key. The robot has also started shuffling the keys, and it moves them too fast for the human eye to follow. Fortunately, you know the algorithm the robot uses to shuffle the keys and the starting position of the stop key.

Each key is numbered from 00 to 77, and the keys sit on distinct points P0,P1,⋯ ,P7P_0, P_1, \cdots, P_7. The robot reads a sequence of length NN given in advance, one command at a time from the front, and shuffles the keys with each command. A command that encodes the integer 2i+2j2^i + 2^j with i<ji < j swaps the positions of key ii and key jj. That is, if key ii is at PkP_k and key jj is at PlP_l, then key ii moves to PlP_l and key jj moves to PkP_k.

The sequence may contain invalid commands. A command xx is invalid if no pair of integers 0≤i<j≤70 \leq i < j \leq 7 satisfies x=2i+2jx = 2^i + 2^j. When the robot recognizes an invalid command, it ignores it and moves on to the next command.

Input

The first line contains NN (1≤N≤200 0001 \leq N \leq 200\,000), the length of the command sequence.

The second line contains the commands a1,a2,⋯ ,aNa_1, a_2, \cdots, a_N separated by spaces (0≤ai<256=280 \leq a_i < 256 = 2^8).

The third line contains KK (0≤K≤70 \leq K \leq 7), the number of the stop key.

Output

Print tt (0≤t≤70 \leq t \leq 7), the index of the point PtP_t where the stop key is located after all commands are applied.

Examples3

  1. Example 1

    Input
    7
    130 72 130 17 96 66 6
    5
    
    Expected output
    3
    
  2. Example 2

    Input
    7
    20 7 68 9 144 156 40
    6
    
    Expected output
    4
    
  3. Example 3

    Input
    7
    111 49 235 228 172 77 151
    2
    
    Expected output
    2