#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
int a[1000];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  int nn = n;
  for (int i = 0; i < m; ++i) {
    int k, f;
    cin >> k >> f;
    a[k] = f;
    nn = max(nn, k);
  }
  vi sol;
  for (int i = 1; i <= nn; ++i) {
    int cnt = 0;
    bool bad = false;
    for (int j = 1; j <= nn; j += i) {
      cnt++;
      for (int k = j; k < j + i; ++k)
        if (a[k] && a[k] != cnt) bad = true;
    }
    if (!bad) sol.push_back(i);
  }
  vi ssol;
  for (int i : sol) {
    int cnt = 0;
    for (int j = 1; j <= nn; j += i) {
      cnt++;
      for (int k = j; k < j + i; ++k) a[k] = cnt;
    }
    ssol.push_back(a[n]);
  }
  sort(begin(ssol), end(ssol));
  ssol.resize(unique(begin(ssol), end(ssol)) - ssol.begin());
  if (ssol.size() > 1) return cout << -1, 0;
  cout << ssol.back();
}
