#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int,int>
#define fi first
#define se second
const int N = 1e5+5;
int n,m;
set<int>st[N];
vector<tuple<char,int,int,int>>res;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for(int i = 1; i <= m; i++){
int u,v;
cin >> u >> v;
st[u].insert(v);
}
int ans = 0;
for(int i = 1; i <= n; i++){
if(st[i].empty()) continue;
ans += st[i].size();
int u = *st[i].begin();
st[i].erase(u);
if(st[u].size() < st[i].size()) swap(st[i],st[u]);
for(auto it: st[i]) st[u].insert(it);
}
cout << ans;
}