import math N = int(input()) M = int(input()) congruences = [] for _ in range(M): B, C = map(int, input().split()) c = C % B congruences.append((B, c)) if M == 0: print("NaN") else: current_a, current_mod = congruences[0][1], congruences[0][0] has_solution = True for i in range(1, M): B_i, c_i = congruences[i] d = math.gcd(current_mod, B_i) if (c_i - current_a) % d != 0: has_solution = False break new_mod = (current_mod // d) * B_i a = current_mod // d b_mod = B_i // d rhs = (c_i - current_a) // d inv = pow(a, -1, b_mod) t0 = (rhs * inv) % b_mod current_a += current_mod * t0 current_mod = new_mod if not has_solution: print("NaN") else: print(current_a if current_a <= N else "NaN")