// #pragma GCC target("avx2") // #pragma GCC optimize("O3") // #pragma GCC optimize("unroll-loops") #include #define rep(i, n) for (int i = 0; i < (int)n; ++i) #define rrep(i, n) for (int i = (int)n - 1; i >= 0; --i) #define ALL(v) v.begin(), v.end() #define pb push_back #define eb emplace_back #define fi first #define se second using namespace std; using ll = long long; using ld = long double; const array di = {0, 1, 0, -1, 1, 1, -1, -1}, dj = {1, 0, -1, 0, 1, -1, -1, 1}; const array YESNO = {"NO", "YES"}; const array YesNo = {"No", "Yes"}; const array yesno = {"no", "yes"}; void YES(bool b = true) { cout << YESNO[b] << '\n'; } void Yes(bool b = true) { cout << YesNo[b] << '\n'; } void yes(bool b = true) { cout << yesno[b] << '\n'; } template inline bool chmax(T& a, const U& b) { if (a < b){ a = b; return true; } return false; } template inline bool chmin(T& a, const U& b) { if (a > b) { a = b; return true; } return false; } template T MAX(const vector& v) { return *max_element(v.begin(), v.end()); } template T MIN(const vector& v) { return *min_element(v.begin(), v.end()); } template T SUM(const vector& v) { return accumulate(v.begin(), v.end(), T()); } template void UNIQUE(vector& v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); } template T POW(T a, int n) { T res = 1; for (; n > 0; n >>= 1, a *= a) if (n & 1) res *= a; return res; } template int lb(const vector& v, T x) { return distance(v.begin(), lower_bound(v.begin(), v.end(), x)); } template int ub(const vector& v, T x) { return distance(v.begin(), upper_bound(v.begin(), v.end(), x)); } /** * @brief 多次元 vector の作成 * @author えびちゃん */ namespace detail { template auto make_vec(vector& sizes, T const& x) { if constexpr (N == 1) { return vector(sizes[0], x); } else { int size = sizes[N-1]; sizes.pop_back(); return vector(size, make_vec(sizes, x)); } } } template auto make_vec(int const(&sizes)[N], T const& x = T()) { vector s(N); for (int i = 0; i < N; ++i) s[i] = sizes[N-i-1]; return detail::make_vec(s, x); } template ostream& operator<<(ostream& os, const vector& v) { for (auto it = v.begin(); it != v.end(); ++it) { if (it == v.begin()) os << *it; else os << ' ' << *it; } return os; } template ostream& operator<<(ostream& os, const pair& p) { os << p.first << ' ' << p.second; return os; } __attribute__((constructor)) void setup() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); } // #include // using namespace atcoder; // using mint = modint1000000007; // using mint = modint998244353; struct unionfind { vector d; unionfind(int n) : d(n, -1) {} int find(int x) { if (d[x] < 0) return x; return d[x] = find(d[x]); } bool unite(int x, int y) { x = find(x); y = find(y); if (x == y) return false; if (d[x] > d[y]) swap(x, y); d[x] += d[y]; d[y] = x; return true; } bool same(int x, int y) { return find(x) == find(y); } int size(int x) { return -d[find(x)]; } }; int main() { int n, m; cin >> n >> m; vector box(n), color(n); rep(i, n) cin >> box[i] >> color[i], box[i]--, color[i]--; unionfind uf(n); map> mp; rep(i, n) mp[color[i]].pb(box[i]); for (auto& [c, v] : mp) { rep(i, v.size() - 1) uf.unite(v[i], v[i+1]); } ll ans = 0; rep(i, n) { if (uf.find(i) == i) { ans += uf.size(i) - 1; } } cout << ans << '\n'; // for (auto& [b, v] : mp) { // sort(ALL(v)); // cout << b << " : " << v << '\n'; // } }