#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 7;
int low[MAXN], dfstime, ans = INT_MAX, id[MAXN], scc, cnt[MAXN];
vector <int> a[MAXN];
int n, m;
bool mark[MAXN];
stack <int> st;
void dfs(int u){
low[u] = id[u] = ++dfstime;
st.push(u);
for(auto v : a[u]){
if(!mark[v]){
if(!id[v]){
dfs(v);
low[u] = min(low[v], low[u]);
}
else low[u] = min(low[u], id[v]);
}
}
if(low[u] == id[u]){
int v;
scc++;
do{
cnt[scc]++;
v = st.top();
st.pop();
mark[v] = 1;
}while(u != v);
}
}
int main(){
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
cin >> n >> m;
for(int i = 1; i <= m; i++){
int x, y;
cin >> x >> y;
a[x].push_back(y);
}
for(int i = 1; i <= n; i++) if(!id[i]) dfs(i);
for(int i = 1; i <= scc; i++) if(cnt[i] > 1)ans = min(ans, cnt[i]);
cout << ans;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnR5cGVkZWYgbG9uZyBsb25nIGxsOwpjb25zdCBpbnQgTUFYTiA9IDFlNSArIDc7CmludCBsb3dbTUFYTl0sIGRmc3RpbWUsIGFucyA9IElOVF9NQVgsIGlkW01BWE5dLCBzY2MsIGNudFtNQVhOXTsKdmVjdG9yIDxpbnQ+IGFbTUFYTl07CmludCBuLCBtOwpib29sIG1hcmtbTUFYTl07CnN0YWNrIDxpbnQ+IHN0Owp2b2lkIGRmcyhpbnQgdSl7CiAgICBsb3dbdV0gPSBpZFt1XSA9ICsrZGZzdGltZTsKICAgIHN0LnB1c2godSk7CiAgICBmb3IoYXV0byB2IDogYVt1XSl7CiAgICAgICAgaWYoIW1hcmtbdl0pewogICAgICAgICAgICBpZighaWRbdl0pewogICAgICAgICAgICAgICAgZGZzKHYpOwogICAgICAgICAgICAgICAgbG93W3VdID0gbWluKGxvd1t2XSwgbG93W3VdKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIGxvd1t1XSA9IG1pbihsb3dbdV0sIGlkW3ZdKTsKICAgICAgICB9CiAgICB9CiAgICBpZihsb3dbdV0gPT0gaWRbdV0pewogICAgICAgIGludCB2OwogICAgICAgIHNjYysrOwogICAgICAgIGRvewogICAgICAgICAgIGNudFtzY2NdKys7CiAgICAgICAgICAgdiA9IHN0LnRvcCgpOwogICAgICAgICAgIHN0LnBvcCgpOwogICAgICAgICAgIG1hcmtbdl0gPSAxOwogICAgICAgIH13aGlsZSh1ICE9IHYpOwogICAgfQp9CgppbnQgbWFpbigpewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTsKICAgIGNvdXQudGllKDApOwogICAgY2luLnRpZSgwKTsKICAgIGNpbiA+PiBuID4+IG07CiAgICBmb3IoaW50IGkgPSAxOyBpIDw9IG07IGkrKyl7CiAgICAgICAgaW50IHgsIHk7CiAgICAgICAgY2luID4+IHggPj4geTsKICAgICAgICBhW3hdLnB1c2hfYmFjayh5KTsKICAgIH0KICAgIGZvcihpbnQgaSA9IDE7IGkgPD0gbjsgaSsrKSBpZighaWRbaV0pIGRmcyhpKTsKICAgIGZvcihpbnQgaSA9IDE7IGkgPD0gc2NjOyBpKyspIGlmKGNudFtpXSA+IDEpYW5zID0gbWluKGFucywgY250W2ldKTsKICAgIAogICAgY291dCA8PCBhbnM7Cn0=