1 #include<cstdio>
2 #include<iostream>
3 #include<algorithm>
4 #include<cstring>
5 #include<queue>
6 #include<cmath>
7 #include<vector>
8 #include<stack>
9 #include<map>
10 #include<bitset>
11 #define rg register
12 #define il inline
13 #define Run(i,l,r) for(int i=l;i<=r;i++)
14 #define Don(i,l,r) for(int i=l;i>=r;i--)
15 #define ll long long
16 #define ld long double
17 #define inf 0x3f3f3f3f
18 using namespace std;
19 const int N=30001 , M=100010;
20 bitset<N>f[N];
21 int n,m,o,hd[N],st[N],id[N],d[N],idx,q[N],t,w;
22 vector<int>g[N];
23 char gc(){
24 static char*p1,*p2,s[1000000];
25 if(p1==p2)p2=(p1=s)+fread(s,1,1000000,stdin);
26 return(p1==p2)?EOF:*p1++;
27 }
28 int rd(){
29 int x=0; char c=gc();
30 while(c<‘0‘||c>‘9‘)c=gc();
31 while(c>=‘0‘&&c<=‘9‘)x=(x<<1)+(x<<3)+c-‘0‘,c=gc();
32 return x;
33 }
34 il bool cmp(const int &a,const int &b){return id[a]<id[b];}
35 il void topsort(){
36 for(rg int i=1;i<=n;i++)if(!d[i])q[++w]=i;
37 while(t<w){
38 int u=q[++t];
39 st[id[u]=++idx]=u;
40 for(rg int i=0;i<(int)g[u].size();i++){
41 int v=g[u][i];
42 if(!--d[v]){
43 q[++w]=v;
44 }
45 }
46 }
47 }
48 int main(){
49 freopen("in.in","r",stdin);
50 freopen("out.out","w",stdout);
51 n=rd(); m=rd();
52 for(rg int i=1,u,v;i<=m;i++){
53 u=rd(); v=rd();
54 g[u].push_back(v);
55 d[v]++;
56 }
57 topsort();
58 for(rg int i=1;i<=n;i++){
59 sort(g[i].begin(),g[i].end(),cmp);
60 }
61 int ans=0;
62 for(rg int i=n;i;i--){
63 int u=st[i];
64 for(rg int j=0;j<(int)g[u].size();j++){
65 int v=g[u][j];
66 if(f[u].test(id[v])){
67 ans++;
68 }else{
69 f[u][id[v]]=1;
70 f[u]|=f[v];
71 }
72 }
73 }
74 cout<<ans<<endl;
75 return 0;
76 }//by tkys_Austin;