/*************************************************
Author :supermaker
Created Time :2016/1/22 21:06:14
File Location :C:\Users\abcd\Desktop\TheEternalPoet
**************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <set>
#include <bitset>
#include <list>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <string>
#include <vector>
#include <ios>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <utility>
#include <complex>
#include <numeric>
#include <functional>
#include <cmath>
#include <ctime>
#include <climits>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cassert>
using namespace std;
#define CFF freopen ("aaa.txt", "r", stdin)
#define CPPFF ifstream cin ("aaa.txt")
#define DB(ccc) cout << #ccc << " = " << ccc << endl
#define PB push_back
#define MP(A, B) make_pair(A, B)
typedef long long LL;
typedef unsigned long long ULL;
typedef double DB;
typedef pair <int, int> PII;
typedef pair <int, bool> PIB;
const int INF = 0x7F7F7F7F;
const int MOD = 1e9 + 7;
const double EPS = 1e-10;
const double PI = 2 * acos (0.0);
const int maxn = 10000 + 66;
int dfn[maxn], low[maxn], belong[maxn], outdeg[maxn], num[maxn];
int n, m, id, tot;
bool instack[maxn];
vector <int> g[maxn];
stack <int> sta;
void dfs (int u)
{
dfn[u] = low[u] = ++id;
instack[u] = true;
sta.push (u);
for (int i = 0; i < g[u].size (); i++)
{
int v = g[u][i];
if (!dfn[v])
{
dfs (v);
low[u] = min (low[u], low[v]);
}
else if (instack[v])
low[u] = min (low[u], dfn[v]);
}
if (low[u] == dfn[u])
{
tot++;
int tt;
do
{
tt = sta.top (); sta.pop ();
instack[tt] = false;
belong[tt] = tot;
num[tot]++;
} while (!sta.empty () && tt != u);
}
}
void SS ()
{
memset (dfn, 0, sizeof (dfn));
memset (low, 0, sizeof (low));
memset (num, 0, sizeof (num));
memset (instack, false, sizeof (instack));
tot = id = 0;
while (!sta.empty ()) sta.pop ();
for (int i = 1; i <= n; i++)
if (!dfn[i])
dfs (i);
}
int main()
{
//CFF;
//CPPFF;
while (scanf ("%d%d", &n, &m) != EOF)
{
for (int i = 0; i < maxn; i++)
g[i].clear ();
for (int i = 1; i <= m; i++)
{
int u, v;
scanf ("%d%d", &u, &v);
g[u].push_back (v);
}
SS ();
memset (outdeg, 0, sizeof (outdeg));
for (int u = 1; u <= n; u++)
for (int i = 0; i < g[u].size (); i++)
{
int v = g[u][i];
if (belong[u] != belong[v])
outdeg[belong[u]]++;
}
int res = 0, pos = 1;
for (int i = 1; i <= tot; i++)
if (!outdeg[i])
pos = i, res++;
if (res == 1) printf ("%d\n", num[pos]);
else puts ("0");
}
return 0;
}