/*
TASK:zala
LANG:C
*/
#include <stdio.h>

typedef struct {int from,to;}links;
links a[50002];
int used[50002],newID[50002],IDnew[50002],usedl[50002];
int n,k,ans=0,br=1,fl[50002]={0},st[50002];

void rec (int);

int main ()
{
    int i,j;
    
    scanf("%d %d",&n,&k);
    for (i=0;i<k;i++)
        scanf ("%d %d",&a[i].from,&a[i].to);
        
    used[1]=1;
    newID[++newID[0]]=0;
    IDnew[0]=br++;
    st[++st[0]]=1;
    rec (1);
    
    printf ("%d\n",ans);
    
    return 0;
}

//===================
void rec (int ind)
{
    int i,j;
    if (ind>n) return;
    
    for (i=0;i<k;i++)
        {
            if (usedl[i]) continue;
            if (a[i].to==newID[ind])
                {
                    usedl[i]=1;
                    fl[ind]=1;
                    if (IDnew[a[i].from]==0)
                        {
                            IDnew[a[i].from]=br++;
                            newID[++newID[0]]=a[i].from;
                        }
                    if (used[IDnew[a[i].from]])
                        for (j=st[0];st[j]!=IDnew[a[i].from];j--)
                            used[st[j]]=2;
                    else if (!used[IDnew[a[i].from]])
                        {
                            used[IDnew[a[i].from]]=1;
                            st[++st[0]]=IDnew[a[i].from];
                            rec (IDnew[a[i].from]);
                            st[0]--;
                            if (used[IDnew[a[i].from]]==1 && fl[IDnew[a[i].from]]) 
                                {
                                    ans++;
                                }
                        }
                }
            if (a[i].from==newID[ind])
                {
                    usedl[i]=1;
                    fl[ind]=1;
                    if (IDnew[a[i].to]==0)
                        {
                            IDnew[a[i].to]=br++;
                            newID[++newID[0]]=a[i].to;
                        }
                    if (used[IDnew[a[i].to]])
                        for (j=st[0];st[j]!=IDnew[a[i].to];j--)
                            used[j]=2;
                    else if (!used[IDnew[a[i].to]])
                        {
                            used[IDnew[a[i].to]]=1;
                            st[++st[0]]=IDnew[a[i].to];
                            rec (IDnew[a[i].to]);
                            st[0]--;
                            if (used[IDnew[a[i].to]]==1 && fl[IDnew[a[i].to]]) 
                                {
                                    ans++;
                                }
                        }
                }
        }
    
    return;
}
//===================
