#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<ctime>
using namespace std;
const int N=10005;
int n,m,cnt,rcnt,head[N],rhead[N],t,rt,ans;
bool vis[N],rvis[N];
struct Node{
int u,v,next;
}edge[N];
struct rNode{
int u,v,next;
}redge[N];
struct Node1{
int id,val;
}f[N];
bool cmp(Node1 p,Node1 q){
return p.val>q.val;
}
void push(int u,int v){
++cnt;
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt;
}
void rpush(int u,int v){
++rcnt;
redge[rcnt].u=u;
redge[rcnt].v=v;
redge[rcnt].next=rhead[u];
rhead[u]=rcnt;
}
void kosaraju(int u){
vis[u]=true;
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].v;
if(!vis[v]){
kosaraju(v);
}
}
f[u].val=++rt;
}
void rkosaraju(int u){
vis[u]=true;
for(int i=rhead[u];i!=-1;i=redge[i].next){
int v=redge[i].v;
if(!vis[v]){
rkosaraju(v);
}
}
}
int main(){
memset(head,-1,sizeof(head));
memset(rhead,-1,sizeof(rhead));
memset(vis,false,sizeof(vis));
int u,v;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%d%d",&u,&v);
push(u,v);
rpush(v,u);
}
for(int i=1;i<=n;i++){
f[i].id=i;
if(!vis[i]){
kosaraju(i);
}
}
memset(vis,false,sizeof(vis));
sort(f+1,f+1+n,cmp);
for(int i=1;i<=n;i++){
if(!vis[f[i].id]){
rkosaraju(f[i].id);
++ans;
}
}
printf("%d",ans);
return 0;
}