#include <iostream>
using namespace std;
int T,p,x,y;
bool b[10001],u=false,v=false;
void dfs(int k)
{
if(u&&v) {
b[x]=1;
b[y]=1;
}
if(k%2==1){
x=(x+y)%p;
if(b[x]){
cout<<"error"<<endl;
return;
}
if(x==0)
{
cout<<"1"<<endl;
return;
}
u=true;
}
else {
y=(x+y)%p;
if(b[y]){
cout<<"error"<<endl;
return;
}
if(y==0) {
cout<<"2"<<endl;
return;
}
v=true;
}
dfs(k+1);
}
int main() {
cin>>T>>p;
while(T--)
{
for(int i=1;i<=9999;i++) b[i]=0;
u=false;
v=false;
cin>>x>>y;
dfs(1);
}
return 0;
}