#include #include #include using namespace std; using ll=long long; #define rep(i,n) for(int i=0;i<(n);i++) #define rrep(i,n) for(int i=(n)-1;i>=0;i--) #define all(v) v.begin(),v.end() #define rall(v) v.rbegin(),v.rend() template bool chmax(T &a, T b){if (a < b){a = b;return true;} else return false;} template bool chmin(T &a, T b){if (a > b){a = b;return true;} else return false;} struct UnionFind { vector par, siz; UnionFind(int n) : par(n,-1), siz(n,1) {} int find(int x){ if (par[x] == -1) return x; else{ par[x] = find(par[x]); return par[x]; } } bool same(int x, int y){ return find(x) == find(y); } int size(int x){ return siz[find(x)]; } void merge(int x, int y){ x = find(x); y = find(y); if (x == y) return; if (siz[x] < siz[y]) swap(x,y); siz[x] += siz[y]; par[y] = x; } }; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N,M; cin>>N>>M; UnionFind uf(N); vectorflag(N); vectorC(N); for(int i=0;i>u>>v; u--; v--; if(u==v)flag[u]=true; uf.merge(u,v); C[u]++; C[v]--; } vector>G(N); for(int i=0;i>w; for(auto i:v)w.push_back(make_pair(C[i],i)); sort(all(w)); int a=0,b=0; for(auto i:w){ if(i==w.front()){ a+=max(0,-1-C[i.second]); b+=max(0,-C[i.second]); }else if(i==w.back()){ a+=max(0,1-C[i.second]); b+=max(0,-C[i.second]); }else{ a+=max(0,-C[i.second]); b+=max(0,-C[i.second]); } } ans+=min(a,b); } ans--; cout<