def inv_gcd(a,b): a=a%b if a==0: return (b,0) s=b;t=a m0=0;m1=1 while(t): u=s//t s-=t*u m0-=m1*u s,t=t,s m0,m1=m1,m0 if m0<0: m0+=b//s return (s,m0) def crt(r,m,nn): assert len(r)==len(m) n=len(r) r0=0;m0=1 for i in range(n): assert 1<=m[i] r1=r[i]%m[i] m1=m[i] if m0nn: return (0,0) return (r0,m0) def main(): n = int(input()) m = int(input()) M, R = [], [] for _ in range(m): mm, rr = map(int, input().split()) rr %= mm M.append(mm) R.append(rr) r, m = crt(R, M, n) if r == 0 and m == 0: print("NaN") else: print(r) if __name__ == '__main__': main()