#include int main () { int n = 0; int m = 0; int b[100000] = {}; int c[100000] = {}; int res = 0; int prev[100001] = {}; int ap[100001] = {}; int cnt = 0; int is_ok = 1; int ans = 0; res = scanf("%d", &n); res = scanf("%d", &m); for (int i = 0; i < m; i++) { res = scanf("%d", b+i); res = scanf("%d", c+i); if (c[i] < 0) { c[i] += (1+(-c[i])/b[i])*b[i]; } if (prev[b[i]] > 0) { int idx = prev[b[i]]-1; if (c[i]%b[i] != c[idx]%b[idx]) { is_ok = 0; } } else { for (int j = c[i]%b[i]; j <= n; j += b[i]) { ap[j]++; } cnt++; } prev[b[i]] = i+1; } if (is_ok <= 0) { printf("NaN\n"); return 0; } while (ans <= n) { if (ap[ans] >= cnt) { printf("%d\n", ans); return 0; } ans++; } printf("NaN\n"); return 0; }