local n = io.read("*n") local m = io.read("*n") local t = {} local lim = 100000 for i = 1, lim do t[i] = false end for i = 1, m do local a, b = io.read("*n", "*n") b = (b + a * 100010) % a if t[a] then if t[a] ~= b then print("NaN") os.exit() end else t[a] = b end end do local f = true for i = 1, lim do if t[i] and t[i] ~= 0 then f = false break end end if f then print(0) os.exit() end end local f = {} for i = 1, n do f[i] = 0 end local need = 0 for i = 1, lim do if t[i] then need = need + 1 local b = t[i] local v = b if v == 0 then v = i end while v <= n do f[v] = f[v] + 1 v = v + i end end end for i = 1, n do if f[i] == need then print(i) os.exit() end end print("NaN")