//#define _GLIBCXX_DEBUG #include using namespace std; using ll = long long; #define pp pair #define rep(i,n) for(int (i)=0;(i)<(n);(i)++) #define ld long double #define al(a) (a).begin(),(a).end() #define mk make_pair #define check cout<<"?"<>t; vector pr(5000001,1); pr[1]=0; for(ll i=2;i<=5000000;i++){ if(pr[i]==0) continue; for(ll j=2*i;j<=5000000;j+=i) pr[j]=0; } rep(_,t){ ll a,p; cin>>a>>p; if(pr[p]==0){ printf("-1\n"); continue; } a%=p; if(a==0){ printf("0\n"); continue; } printf("1\n"); } }