n = int(input()) m = int(input()) BC = [] dic = {} for i in range(m): b,c = map(int,input().split()) c %= b if b in dic and dic[b] != c: print("NaN") exit() dic[b] = c BC.append([b,c]) nums = [0]*(n+1) for b,c in BC: for i in range(c,n+1,b): nums[i] += 1 le = len(BC) for i in range(n+1): if nums[i] == le: print(i) exit() print("NaN")