#include <bits/stdc++.h>
using namespace std;
const long double pi = 3.14159265359;
template <typename T>
T abs(T x) {
  return x > 0 ? x : -x;
}
template <typename T>
T sqr(T x) {
  return x * x;
}
template <typename T>
void chmin(T &x, T y) {
  x = min(x, y);
}
template <typename T>
void chmax(T &x, T y) {
  x = max(x, y);
}
template <typename U, typename V>
ostream &operator<<(ostream &s, const pair<U, V> &x) {
  s << "(" << x.first << ", " << x.second << ")";
  return s;
}
template <typename U>
ostream &operator<<(ostream &s, const vector<U> &x) {
  s << "[";
  bool was = false;
  for (auto it : x) {
    if (was) {
      s << ", ";
    }
    was = true;
    s << it;
  }
  s << "]";
  return s;
}
template <typename U>
ostream &operator<<(ostream &s, const set<U> &x) {
  s << "{";
  bool was = false;
  for (auto it : x) {
    if (was) {
      s << ", ";
    }
    was = true;
    s << it;
  }
  s << "}";
  return s;
}
const int maxn = 1e5 + 5;
vector<pair<int, int>> g[maxn];
int used[maxn];
int c[maxn];
int cu = 1;
int ub = -1;
void dfs(int v) {
  used[v] = cu;
  for (auto to : g[v]) {
    int nbal = c[v] + to.second;
    if (used[to.first] == cu) {
      if ((nbal - c[to.first]) != 0) {
        ub = abs(nbal - c[to.first]);
      }
    } else {
      c[to.first] = nbal;
      dfs(to.first);
    }
  }
}
bool bad = false;
int mod;
void dfs2(int v) {
  used[v] = cu;
  for (auto to : g[v]) {
    if (used[to.first] == cu) {
      bad |= ((c[v] + to.second + mod) % mod) != c[to.first];
    } else {
      c[to.first] = (c[v] + to.second + mod) % mod;
      dfs2(to.first);
    }
  }
}
int main() {
  srand(time(NULL));
retry:
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    g[u].push_back(make_pair(v, 1));
    g[v].push_back(make_pair(u, -1));
  }
  cu++;
  for (int i = 1; i <= n; i++) {
    if (used[i] != cu) {
      dfs(i);
    }
  }
  if (ub == -1) {
    cout << n << endl;
    return 0;
  }
  for (int i = ub; i >= 1; i--) {
    if (ub % i == 0) {
      cu++;
      fill(c, c + maxn, 0);
      mod = i;
      bad = false;
      for (int j = 1; j <= n; j++) {
        if (used[j] != cu) {
          dfs2(j);
        }
      }
      if (!bad) {
        cout << i << endl;
        return 0;
      }
    }
  }
  return 0;
}
