#include using namespace std; using ll = long long; class UnionFind { int n; vector parent; vector edgeCnt; public: UnionFind(int n_) : n(n_) { parent.resize(n); edgeCnt.resize(n, 0); iota(parent.begin(), parent.end(), 0); } int find(int x) { int y = parent[x]; if (y != parent[y]) { y = find(y); } return parent[x] = y; } void unite(int a, int b) { int x = find(a); int y = find(b); if (x != y) { parent[y] = x; edgeCnt[x] = edgeCnt[y] + 1; } else { edgeCnt[x]++; } } bool same(int a, int b) { return find(a) == find(b); } int EdgeCnt(int i) { return edgeCnt[i]; } }; int main() { int N, M; cin >> N >> M; vector u(M), v(M); vector inDegree(N, 0); vector outDegree(N, 0); UnionFind uf(N); for (int i = 0; i < M; i++) { cin >> u[i] >> v[i]; u[i]--; v[i]--; inDegree[v[i]]++; outDegree[u[i]]++; uf.unite(u[i], v[i]); } vector cnt(N, 0); for (int i = 0; i < N; i++) { cnt[uf.find(i)] += abs(inDegree[i] - outDegree[i]); } int ans = -1; for (int i = 0; i < N; i++) { if (uf.find(i) == i) { if (uf.EdgeCnt(i) > 0) { ans += max(0, cnt[i] / 2 - 1); ans++; } } } cout << ans << endl; }