#include<bits/stdc++.h> usingnamespace std; #define ull unsigned long long
inlineintread(){ int x = 0, f = 0; char c = 0; while (!isdigit(c)) f |= c == '-', c = getchar(); while (isdigit(c)) x = x * 10 + (c & 15), c = getchar(); return f ? -x : x; }
#define N 1000005 #define M 2000005 int n, m; ull hs[N], val[M]; mt19937_64 rd(time(0));
structtrinum { int a[2188][2188]; trinum() { memset(a, 0, sizeof a); for (int i = 0; i < 2187; i ++) { for (int j = 0; j < 2187; j ++) { int x = i, y = j, v = 1; for (int k = 0; k < 7; k ++) { int t = (x % 3 + y % 3) % 3; a[i][j] += t * v, x /= 3, y /= 3, v *= 3; } } } } ull ran(){ returnrd() % 12157665459056928801ull; } ull tor(ull x, ull y)const{ ull r = 0, v = 1; for (int i = 0; i < 6; i ++) { r += a[x % 2187][y % 2187] * v; x /= 2187, y /= 2187, v *= 2187; } return r; } } trin;
intmain(){ srand(time(NULL)); n = read(), m = read(); for (int i = 1; i <= n; i ++) { hs[i] = trin.ran(); hat.add(hs[i], i); hat.add(trin.tor(hs[i], hs[i]), i); } val[0] = 0; for (int i = 1, la = 0; i <= m; i ++) { int u = read() ^ la, fa = read() ^ la; val[i] = trin.tor(hs[u], val[fa]); la = val[i] ? hat.ask(val[i]) : -1; printf("%d\n", la); } return0; }