#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
vector<pair<int, bool> > E[N];
bool u[N];
int dep[N];
void dfs(int x) {
  u[x] = true;
  int i, j;
  for (i = E[x].size() - 1; i >= 0; i--) {
    j = E[x][i].first;
    if (!u[j]) {
      if (E[x][i].second)
        dep[j] = dep[x] + 1;
      else
        dep[j] = dep[x] - 1;
      dfs(j);
    }
  }
}
int gcd(int x, int y) {
  int z;
  while (y) {
    z = x % y;
    x = y;
    y = z;
  }
  return x;
}
int main() {
  int n, m, i;
  scanf("%d%d", &n, &m);
  for (i = 0; i < m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    E[a].push_back(make_pair(b, 1));
    E[b].push_back(make_pair(a, 0));
  }
  int ret = -1;
  for (i = 1; i <= n; i++)
    if (!u[i]) dfs(i);
  for (i = 1; i <= n; i++) {
    for (int j = E[i].size() - 1; j >= 0; j--) {
      int x = E[i][j].first, tmp;
      if (E[i][j].second)
        tmp = dep[i] + 1;
      else
        tmp = dep[i] - 1;
      if (dep[x] != tmp) {
        tmp = dep[x] > tmp ? dep[x] - tmp : tmp - dep[x];
        if (ret == -1)
          ret = tmp;
        else
          ret = gcd(ret, tmp);
      }
    }
  }
  if (ret == -1) ret = n;
  printf("%d\n", ret);
  return 0;
}
