import random import sys input = lambda: sys.stdin.readline().rstrip() ii = lambda: int(input()) mi = lambda: map(int, input().split()) li = lambda: list(mi()) inf = 2 ** 63 - 1 mod = 998244353 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 inv_mod(x,m): assert 1<=m z=inv_gcd(x,m) assert z[0]==1 return z[1] def crt(r,m): 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 m0 N: return (-1, 0) return (r0,m0) def floor_sum(n,m,a,b): ans=0 if a>=m: ans+=(n-1)*n*(a//m)//2 a%=m if b>=m: ans+=n*(b//m) b%=m y_max=(a*n+b)//m x_max=(y_max*m-b) if y_max==0: return ans ans+=(n-(x_max+a-1)//a)*y_max ans+=floor_sum(y_max,a,m,(a-x_max%a)%a) return ans N = ii() M = ii() r = [] m = [] BC = [] for i in range(M): b, c = mi() BC.append((b, c)) random.shuffle(BC) for b, c in BC: r.append(c) m.append(b) C = crt(r[:1000], m[:1000]) if C[1] == 0 or C[0] > N: print('NaN') exit() else: ans = C[0] for b, c in BC: if (ans - c) % b != 0: print('NaN') exit() print(ans)