import java.io.*;
import java.util.Arrays;
public class Main {
static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static final int N = 10010, M = 50010;
static int[] h = new int[N], e = new int[M], ne = new int[M];
static int[] dfn = new int[N], low = new int[N];
static int[] stk = new int[N], id = new int[N];
static int[] size = new int[N];
static int[] out = new int[N];
static boolean[] in_stk = new boolean[N];
static int n, t, idx, time, scc_cnt, top;
public static void main(String[] args) throws Exception {
Arrays.fill(h, -1);
n = getInt(); t = getInt();
while (t-- > 0) {
int a = getInt(), b = getInt();
add(a, b);
}
for (int i = 1; i <= n; i++) {
if (dfn[i] == 0) {
tarjan(i);
}
}
for (int i = 1; i <= n; i++) {
for (int j = h[i]; j != -1; j = ne[j]) {
int k = e[j];
int a = id[i], b = id[k];
if (a != b) {
out[a]++;
}
}
}
int zeros = 0;
int sum = 0;
for (int i = 1; i <= scc_cnt; i++) {
if (out[i] == 0) {
zeros++;
sum += size[i];
if (zeros > 1) {
sum = 0;
break;
}
}
}
pw.println(sum);
pw.flush();
}
static void tarjan(int u) {
dfn[u] = low[u] = ++time;
stk[++top] = u; in_stk[u] = true;
for (int i = h[u]; i != -1; i = ne[i]) {
int j = e[i];
if (dfn[j] == 0) {
tarjan(j);
low[u] = Math.min(low[u], low[j]);
} else if (in_stk[j]) {
low[u] = Math.min(low[u], dfn[j]);
}
}
if (dfn[u] == low[u]) {
int y;
++scc_cnt;
do {
y = stk[top--];
in_stk[y] = false;
id[y] = scc_cnt;
size[scc_cnt]++;
} while (y != u);
}
}
static void add(int a, int b) {
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
static int getInt() throws Exception {
st.nextToken();
return (int) st.nval;
}
}