// Get together #include #include #include #include using namespace std; int main() { int N, M; cin >> N >> M; vector box; vector > color; for (int i = 0; i < N; i++) { int b, c; cin >> b >> c; b--; c--; box.push_back(b); color.push_back(make_pair(c, i)); } sort(color.begin(), color.end()); int ans = 0; for (int i = 1; i < N; i++) { int index = color[i].second; int bef = color[i - 1].second; if (color[i].first == color[i - 1].first && box[index] != box[bef]) { ans++; box[index] = box[bef]; } } cout << ans << endl; return 0; }