P5635
#include<bits/stdc++.h>
using namespace std;
short t,x,y,p,a[10005][10005];
bool b[10005][10005];
string fun(int x,int y){
if(a[x][y]!=0)return to_string((char)(a[x][y]+'0'));
memset(b,0,sizeof(b));
while(x!=0||y!=0){
x=(x+y)%p;
if(x==0){
a[x][y]=1;
return "1";
}
if(a[x][y]!=0)return to_string((char)(a[x][y]+'0'));
if(b[x][y])return "error";
b[x][y]=1;
y=(x+y)%p;
if(y==0){
a[x][y]=2;
return "2";
}
if(a[x][y]!=0)return to_string((char)(a[x][y]+'0'));
if(b[x][y])return "error";
b[x][y]=1;
}
}
signed main(){
cin>>t>>p;
for(int i=1;i<=t;i++){
cin>>x>>y;
cout<<fun(x,y)<<endl;
}
return 0;
}