import sys input = sys.stdin.readline from collections import * def extgcd(a, b): if b: d, y, x = extgcd(b, a % b) y -= (a // b) * x return d, x, y return a, 1, 0 def crt(rs, ms): x = 0; d = 1 for X, Y in zip(rs, ms): g, a, b = extgcd(d, Y) x, d = (Y*b*x + d*a*X) // g, d*(Y // g) x %= d if d>N: exit(print('NaN')) return x, d N = int(input()) M = int(input()) rs, ms = [], [] for _ in range(M): B, C = map(int, input().split()) C %= B rs.append(C) ms.append(B) x, d = crt(rs, ms) for X, Y in zip(rs, ms): if x%Y!=X: print('NaN') exit(0) if x>N: print('NaN') else: print(x)