import sys sys.setrecursionlimit(200005) int1 = lambda x: int(x)-1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] # dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] inf = (1 << 63)-1 # inf = (1 << 31)-1 # md = 10**9+7 md = 998244353 import typing def inv_gcd(a, b): a %= b if a == 0: return b, 0 s, t = b, a m0, m1 = 0, 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 # 複数の「mで割ったらr余る」という条件を満たすxをmod zで返す # 返り値 x,z(解なしの場合は0,0) def crt(r: typing.List[int], m: typing.List[int]) -> typing.Tuple[int, int]: assert len(r) == len(m) n = len(r) r0, m0 = 0, 1 for i in range(n): assert 1 <= m[i] r1 = r[i]%m[i] m1 = m[i] if m0 < m1: r0, r1 = r1, r0 m0, m1 = m1, m0 if m0%m1 == 0: if r0%m1 != r1: return 0, 0 continue g, im = inv_gcd(m0, m1) u1 = m1//g if (r1-r0)%g: return 0, 0 x = (r1-r0)//g%u1*im%u1 r0 += x*m0 m0 *= u1 if r0 < 0: r0 += m0 if r0 > N: return 0, 0 return r0, m0 N = II() m = II() bb, cc = [], [] for _ in range(m): b, c = LI() if b == 1: continue c %= b bb.append(b) cc.append(c) x, z = crt(cc, bb) # print(x,z) if z == 0 or x > N: print("NaN") else: print(x)