Kernel Knights

Find the lexicographically smallest kernel among 2n knights where each knight challenges exactly one knight of the other house.

Hard8GraphGreedyNo attempts yetTime limit2sMemory limit512 MB

Problem

Jousting is a medieval contest in which two people ride horses at high speed and try to strike each other with wooden lances. A total of 2n2n knights entered a tournament, nn from each of two rival houses. On arrival, every knight challenged exactly one knight of the other house to a duel.

A subset SS of the knights is called a kernel when both of these hold.

  • No knight in SS was challenged by another knight in SS.
  • Every knight outside SS was challenged by some knight in SS.

Given all of the challenges, find a kernel. A kernel always exists.

Input

The first line contains the number of knights in each house, nn (1n1000001 \le n \le 100\,000). The knights of the first house are numbered 11 through nn, and the knights of the second house are numbered n+1n+1 through 2n2n.

The second line contains integers f1,f2,,fnf_1, f_2, \dots, f_n, where fkf_k is the index of the knight challenged by knight kk (n+1fk2nn+1 \le f_k \le 2n).

The third line contains integers s1,s2,,sns_1, s_2, \dots, s_n, where sks_k is the index of the knight challenged by knight n+kn+k (1skn1 \le s_k \le n).

Output

Print the indices of the knights in the kernel in increasing order on a single line, separated by single spaces.

If several kernels exist, print the lexicographically smallest one. That is, write each kernel as its increasing sequence of indices and print the sequence that comes first lexicographically.