#include #include #include using namespace std; int N,M; int val[1<<17]; bool isp[1<<17]; main() { cin>>N>>M; for(int i=1;i<=1e5;i++)val[i]=-1; for(int i=0;i>B>>C; C=(C%B+B)%B; if(val[B]==-1)val[B]=C; else if(val[B]!=C) { cout<<"NaN"< >Q; int mv=0; for(int p=2;p<=1e5;p++)if(!isp[p]) { for(int i=p+p;i<=1e5;i+=p)isp[i]=true; vector >x; for(int i=p;i<=1e5;i+=p)if(val[i]!=-1) { int u=i,t=1; while(u%p==0) { u/=p; t*=p; } x.push_back(make_pair(t,val[i]%t)); } if(!x.empty()) { sort(x.begin(),x.end()); int C=x.back().second,B=x.back().first; for(pairp:x) { if(C%p.first!=p.second) { cout<<"NaN"<p:Q) { if(n%p.second!=p.first) { ok=false; break; } } if(ok) { cout<