1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include <bits/stdc++.h>
#include <unordered_map>
#include "kollib.h"
#include "message.h"
//#include <emmintrin.h>

using namespace std;

#define INLINE   inline __attribute__ ((always_inline))

#define FORE(it,c)  for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define FOR(i,a,b)  for(int i=(a);i<(b);++i)
#define REP(i,a)    FOR(i,0,a)
#define ZERO(m)    memset(m,0,sizeof(m))
#define ALL(x)      x.begin(),x.end()
#define PB          push_back
#define S          size()
#define LL          long long
#define ULL        unsigned long long
#define LD          long double
#define MP          make_pair
#define X          first
#define Y          second
#define VC          vector
#define PII        pair <int, int>
#define VI          VC < int >
#define VVI        VC < VI >
#define VD          VC < double >
#define VVD        VC < VD >
#define VS          VC < string >
#define DB(a)      cerr << #a << ": " << (a) << endl;

template<class T> void print(VC < T > v) {cerr << "[";if (v.S) cerr << v[0];FOR(i, 1, v.S) cerr << ", " << v[i];cerr << "]\n";}
template<class T> string i2s(T x) {ostringstream o; o << x; return o.str(); }
VS splt(string s, char c = ' ') {VS all; int p = 0, np; while (np = s.find(c, p), np >= 0) {if (np != p) all.PB(s.substr(p, np - p)); p = np + 1;} if (p < s.S) all.PB(s.substr(p)); return all;}

struct RNG {
    unsigned int MT[624];
    int index;
	
	RNG(int seed = 1) {
		init(seed);
	}
    
    void init(int seed = 1) {
        MT[0] = seed;
        FOR(i, 1, 624) MT[i] = (1812433253UL * (MT[i-1] ^ (MT[i-1] >> 30)) + i);
        index = 0;
    }
    
    void generate() {
        const unsigned int MULT[] = {0, 2567483615UL};
        REP(i, 227) {
            unsigned int y = (MT[i] & 0x8000000UL) + (MT[i+1] & 0x7FFFFFFFUL);
            MT[i] = MT[i+397] ^ (y >> 1);
            MT[i] ^= MULT[y&1];
        }
        FOR(i, 227, 623) {
            unsigned int y = (MT[i] & 0x8000000UL) + (MT[i+1] & 0x7FFFFFFFUL);
            MT[i] = MT[i-227] ^ (y >> 1);
            MT[i] ^= MULT[y&1];
        }
        unsigned int y = (MT[623] & 0x8000000UL) + (MT[0] & 0x7FFFFFFFUL);
        MT[623] = MT[623-227] ^ (y >> 1);
        MT[623] ^= MULT[y&1];
    }
    
    unsigned int rand() {
        if (index == 0) {
            generate();
        }
        
        unsigned int y = MT[index];
        y ^= y >> 11;
        y ^= y << 7  & 2636928640UL;
        y ^= y << 15 & 4022730752UL;
        y ^= y >> 18;
        index = index == 623 ? 0 : index + 1;
        return y;
    }
    
    INLINE int next(int x) {
        return rand() % x;
    }
    
    INLINE int next(int a, int b) {
        return a + (rand() % (b - a));
    }
    
    INLINE double nextDouble() {
        return (rand() + 0.5) * (1.0 / 4294967296.0);
    }
};

static RNG rng;

const int LOW_N = 2000;
const int MAX_M = 200;
const int MAX_N = 1000001000;
const int RP = 500;

int N;
int M;
int QA[MAX_M];
int QB[MAX_M];

int path[RP];
int ID;
int NODES;


VI CN[RP];
VI CX[RP];
int CB[RP+2*MAX_M][RP+2*MAX_M] = {0};
int DD[RP+2*MAX_M];

const int COMPRESSED_BITS = 1;
const int BITS = 32 << COMPRESSED_BITS;
unsigned int IP[MAX_N / BITS + 1];
unordered_map<unsigned int, VI> IPM;

INLINE void addIP(unsigned int x, int t) {
	IP[x / BITS] |= 1U << ((x & (BITS - 1)) >> COMPRESSED_BITS);
	IPM[x].PB(t);
}

INLINE bool checkIP(unsigned int x) {
	return (IP[x / BITS] >> ((x & (BITS - 1)) >> COMPRESSED_BITS)) & 1;
}

// #define cerr cout
int main() {
	assert(sizeof(int) == 4);

	N = NumberOfStudents();
	M = NumberOfQueries();
	REP(i, M) QA[i] = QueryFrom(i+1);
	REP(i, M) QB[i] = QueryTo(i+1);
	
	if (N <= LOW_N) {
		if (MyNodeId() > 0) return 0;
		REP(i, M) {
			if (QA[i] == QB[i]) {
				cout << 0 << endl;
				continue;
			}
			
			int d = 1;
			int lp = QA[i];
			int p = FirstNeighbor(QA[i]);
			while (p != QB[i]) {
				int a = FirstNeighbor(p);
				if (a != lp) {
					lp = p;
					p = a;
				} else {
					lp = p;
					p = SecondNeighbor(p);
				}
				d++;
			}
			cout << min(d, N - d) << endl;
		}
		return 0;
	}
	
	REP(i, RP) {
		while (true) {
			path[i] = rng.next(N) + 1;
			bool ok = true;
			REP(j, i) ok &= path[i] != path[j];
			REP(j, M) ok &= path[i] != QA[j];
			REP(j, M) ok &= path[i] != QB[j];
			if (ok) break;
		}
	}
	
	ID = MyNodeId();
	NODES = NumberOfNodes();
	
	REP(i, RP) addIP(path[i], i);
	REP(i, M) addIP(QA[i], RP + i);
	REP(i, M) addIP(QB[i], RP + M + i);
	
	
	if (ID > 0) {
		int cp = ID - 1;
		VI msg;
		while (cp < RP) {
			int op = path[cp];
			int p, lp, d;
			
			REP(dir, 2) {
				p = dir == 0 ? FirstNeighbor(op) : SecondNeighbor(op);
				lp = op;
				d = 0;
				while (true) {
					d++;
					
					if (checkIP(p)) {
						bool done = false;
						for (int x : IPM[p]) {
							// PutInt(0, cp);
							// PutInt(0, x);
							// PutInt(0, d);
							// Send(0);
							msg.PB(cp);
							msg.PB(x);
							msg.PB(d);
							done |= x < RP;
						}
						if (done) break;
					}
				
					int a = FirstNeighbor(p);
					if (a != lp) {
						lp = p;
						p = a;
					} else {
						lp = p;
						p = SecondNeighbor(p);
					}
				}
			}			
			
			cp += NODES - 1;
		}
		
		PutInt(0, msg.S / 3);
		REP(i, msg.S) PutInt(0, msg[i]);
		Send(0);
	
		return 0;
	} else {
		// REP(i, 2 * RP + 4 * M) {
			// int x = Receive(-1);
			// int a = GetInt(x);
			// int b = GetInt(x);
			// int c = GetInt(x);
			// CN[a].PB(b);
			// CX[a].PB(c);
			// CB[b][a] = 1;
		// }
		
		REP(i, NODES - 1) {
			int x = Receive(-1);
			int msgno = GetInt(x);
			while (msgno--) {
				int a = GetInt(x);
				int b = GetInt(x);
				int c = GetInt(x);
				CN[a].PB(b);
				CX[a].PB(c);
				CB[b][a] = 1;
			}
		}
		
		
		int p = 0;
		int lp = 0;
		for (int x : CN[0]) if (x < RP) {
			p = x;
			break;
		}
		int op = p;
		
		int d = 0;
		do {
			// cerr << "P: " << p << endl;
		
			REP(i, CN[p].S) if (CN[p][i] < RP && CN[p][i] != lp) {
				d += CX[p][i];
				lp = p;
				p = CN[p][i];
				break;
			}
			
			REP(i, CN[p].S) {
				if (CN[p][i] < RP) continue;
				if (!CB[CN[p][i]][p]) continue;
				if (!CB[CN[p][i]][lp]) continue;
				DD[CN[p][i] - RP] = d - CX[p][i];
			}
			
		} while (p != op);
		
		// cerr << "D: " << d << endl;
		
		REP(i, M) {
			int ad = abs(DD[i] - DD[M + i]);
			cout << min(ad, N - ad) << endl;
		}
	}
	
	return 0;
}
// #undef cerr