def extend_euclidean_algorithm(a, b): if b == 0: return 1, 0 y, x = extend_euclidean_algorithm(b, a%b) y -= (a//b)*x return x, y def chinese_remainder_theorem(s, p, t, q): d = gcd(p, q) if (s-t)%d: return -1, -1 x, y = extend_euclidean_algorithm(p, q) return (s*q*y+t*p*x)//d, p*q//d from math import gcd n = int(input()) c, b = 0, 1 for _ in range(int(input())): bi, ci = map(int, input().split()) c, b = chinese_remainder_theorem(c, b, ci, bi) if c == -1 or c > n: c = "NaN"; break print(c)