#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 10, maxm = 5e4 + 10, Inf = 0x3f3f3f3f;
int d[maxn], head[maxn], repeat[maxn][maxn];
int len, n, m, ans;
int read() {
int f = 1, k = 0;
char c = getchar();
while (c < '0' || c > '9') {
c = getchar();
}
while (c >= '0' && c <= '9') {
k = k * 10 + (c ^ 48);
c = getchar();
}
return f * k;
}
struct Edge {
int from, to, nxt, w;
} e[maxm];
void add(int u, int v, int w) {
e[++len].from = u;
e[len].to = v;
e[len].w = w;
e[len].nxt = head[u];
head[u] = len;
}
struct Node {
int num, dis;
Node(int x, int y) {
num = x;
dis = y;
}
};
bool operator <(const Node &a, const Node &b) {
return a.dis > b.dis;
}
void dij(int s) {
priority_queue<Node>q;
memset(d, Inf, sizeof d);
q.push(Node(s, 0));
d[s] = 0;
while (!q.empty()) {
int u = q.top().num;
q.pop();
for (int i = head[u]; i; i = e[i].nxt) {
int v = e[i].to;
if (d[v] > d[u] + e[i].w) {
d[v] = d[u] + e[i].w;
q.push(Node(v, d[v]));
}
}
}
}
bool solv() {
for (int i = 1; i <= n; ++i) {
if (d[i] == Inf) {
return 1;
}
}
return 0;
}
int main() {
n = read(), m = read();
int a, b;
for (int i = 1; i <= m; ++i) {
a = read(), b = read();
if (repeat[b][a] == 0) {
add(b, a, 1);
repeat[b][a]++;
}
}
for (int i = 1; i <= n; ++i) {
if (repeat[b][a] == 1) {
dij(i);
if (!solv()) {
ans++;
}
}
}
printf("%d", ans);
return 0;
}