#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <cmath>

#include "kollib.h"
#include "message.h"

using namespace std;

typedef vector<int> VI;
typedef long long LL;

#define FOR(x, b, e) for(int x = b; x <= (e); ++x)
#define FORD(x, b, e) for(int x = b; x >= (e); --x)
#define REP(x, n) for(int x = 0; x < (n); ++x)
#define VAR(v, n) __typeof(n) v = (n)
#define ALL(c) (c).begin(), (c).end()
#define SIZE(x) ((int)(x).size())
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define PB push_back
#define ST first
#define ND second
#define MP make_pair

#define fir FirstNeighbor
#define sec SecondNeighbor

typedef pair<int,int> para;


para last(int a, int b, int n) {
  int c;

  REP(i,n) {
    c=fir(b);
    if (c==a) c=sec(b);
    a=b; b=c;
  }
  return MP(a,b);
}


int main() {
  int n, m, nr, k, a, b, d, n1, s, q, f, t, i0, i1, Q, l=0;
  int w[201];
  para p;

  n=NumberOfStudents();
  m=NumberOfNodes();
  nr=MyNodeId();

//cout<<"nr "<<nr<<" punkt-1";

  Q=NumberOfQueries();
  if (Q==0) return 0;
  if (n<=400) {
      m=3;
      if (nr>2) return 0;
  }
  n1= (nr==m-1 ? 0 : nr+1);
  k=n/m; if (nr<n%m) k++;
//cout<<nr<<endl;

/*
  if (nr==0) {
//cout<<"1 ";
    p=last(sec(1),1,k-1);
  }
  else {
    Receive(nr-1);
    a=GetInt(nr-1);
    p=last(a,GetInt(nr-1),k);
  }
*/
//cout<<"nr "<<nr<<" punkt -2\n";

  if (nr==0) {
    b=sec(1);
    a=sec(b);
    if (a==1) a=fir(b);
  }
  else {
    Receive(nr-1);
    a=GetInt(nr-1);
    b=GetInt(nr-1);
  }

  p=last(a,b,k);
//  int n1= (nr==m-1 ? 0 : nr+1);
  if (n1!=0) {
    PutInt(n1, p.ST);
    PutInt(n1, p.ND);
    Send(n1);
  }


  vector<para> good;
  vector<para>::const_iterator it;
  good.reserve(k+1);

  REP(i,k) {
  int c;
   c=fir(b);
   if (c==a) c=sec(b);
   good.PB(MP(c,i));
   a=b; b=c;
  }
  sort(ALL(good));

  FOR(i,1,Q) {
    f=QueryFrom(i);
//    it=mapa.find(f);
    it=lower_bound(ALL(good), MP(f,0));
    if ( it == good.end() ) continue;
    if (it->ST != f) continue;

    i0=it->ND;
    t=QueryTo(i);
//    it=mapa.find(t);
    it=lower_bound(ALL(good), MP(t,0));


    if (it==good.end() || it->ST != t) {
      PutInt(n1, 0);      //typ wiadomości - licz dalej
      PutInt(n1, i);      //nr zapytania
      PutInt(n1, k-i0);   //czas do pierwszego z nast. przedziału
      Send(n1);
    }
    else {
      i1=it->ND;
      PutInt(0, 1);      //typ wiadomości - wynik
      PutInt(0, i);      //nr zapytania
      PutInt(0, abs(i1-i0));   //czas
      Send(0);
    }
  }

//cout<<"nr "<<nr<<" punkt4\n";   return 0;

  while (true) {
    s=Receive(-1);

    switch (GetInt(s)) {
      case 0:
        q=GetInt(s);
        d=GetInt(s);
        t=QueryTo(q);
//        it=mapa.find(t);
        it=lower_bound(ALL(good), MP(t,0));
        if (it==good.end() || it->ST != t) {
          PutInt(n1, 0);      //typ wiadomości - licz dalej
          PutInt(n1, q);      //nr zapytania
          PutInt(n1, d+k);   //czas do pierwszego z nast. przedziału
          Send(n1);
        }
        else {
          i1=it->ND;
          d+=i1;
          d=min(d,n-d);
          PutInt(0, 1);      //typ wiadomości - wynik
          PutInt(0, q);      //nr zapytania
          PutInt(0, d);   //czas
          Send(0);
        }
      break;

      case 1:                //tylko node 0!
//cout<<"nr "<<nr<<" case 1\n";
        q=GetInt(s);
        d=GetInt(s);
        w[q]=d; l++;
        if (l==Q) {
          FOR(i,1,m-1) {
            PutInt(i, -1);
            Send(i);
          }
          FOR(i,1,Q) cout<<w[i]<<endl;
          return 0;
        }
      break;

      case -1:
//cout<<"nr "<<nr<<" case -1\n";
        return 0;
      default: ;
//cout<<"nr "<<nr<<" case ?\n";
    }
  }
}
