#include <bits/stdc++.h>
using namespace std;
int t,mod,x,y;
long long book[100][100];
int rem(int x,int y)
{
if(book[x][y])
{
return book[x][y];
}
book[x][y]=-1;
if(!x)
{
book[x][y]=1;
return book[x][y];
}
if(!y)
{
book[x][y]=2;
return book[x][y];
}
int numx=(x+y)%mod;
int numy=(numx+y)%mod;
book[x][y]=rem(numx,numy);
return book[x][y];
}
int main()
{
cin>>t>>mod;
for(int i=1;i<=t;i++)
{
memset(book,0,sizeof(book));
cin>>x>>y;
int ans=rem(x,y);
if(ans==-1)
{
cout<<"error"<<endl;
}
else if(ans==1)
{
cout<<"1"<<endl;
}
else
{
cout<<"2"<<endl;
}
}
return 0;
}