#include <iostream>
#include <cctype>
#include <cstring>
#include <set>
#include <queue>
#include <climits>
#include <vector>
#include <iterator>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <stack>
#include <string>
#include <unordered_map>
#include <map>
#include <list>
#include <iomanip>
#include <ctype.h>
#include <stdio.h>
//****DEFINE****//
#define mod 1000000007
#define pie 3.141592653589793238
#define all(x) x.begin(), x.end()
template <typename A, typename B>
A amax(A &a, B b)
{
if (b > a)
a = b;
return a;
}
template <typename A, typename B>
A amin(A &a, B b)
{
if (b < a)
a = b;
return a;
}
using namespace std;
typedef long long ll;
//****FUNCTIONS****//
bool cmd1(pair<ll, ll> a, pair<ll, ll> b)
{
if (a.second > b.second)
{
return true;
}
return false;
}
bool cmd2(int a, int b)
{
if (a > b)
{
return true;
}
return false;
}
//********** TO TAKE INPUT FROM A FILE ************
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int dfs(int root, vector<vector<int> > &adj1, vector<int> &vis)
{
int cnt = 1;
vis[root] = 1;
for (auto it : adj1[root])
{
if (vis[it] == 0)
{
cnt += dfs(it, adj1, vis);
}
}
return cnt;
}
void meena()
{
int n;
cin >> n;
map<string, set<int> > adj;
map<string, set<int> > adj2;
for (int i = 0; i < n; i++)
{
string x, y;
cin >> x >> y;
adj[x].insert(i);
adj2[y].insert(i);
}
vector<vector<int> > adj1(n);
for (auto it : adj)
{
set<int> temp = it.second;
int prev = -1;
for (auto it1 : temp)
{
if (prev == -1)
{
prev = it1;
}
else
{
adj1[it1].push_back(prev);
adj1[prev].push_back(it1);
prev = it1;
}
}
}
for (auto it : adj2)
{
set<int> temp = it.second;
int prev = -1;
for (auto it1 : temp)
{
if (prev == -1)
{
prev = it1;
}
else
{
adj1[it1].push_back(prev);
adj1[prev].push_back(it1);
prev = it1;
}
}
}
vector<int> vis(n, 0);
int cnt = 0;
for (int i = 0; i < n; i++)
{
if (vis[i] == 0)
{
cnt = max(dfs(i, adj1, vis), cnt);
}
}
cout << n - cnt;
cout <<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J0KGkpOwogICAgfQogICAgdmVjdG9yPHZlY3RvcjxpbnQ+ID4gYWRqMShuKTsKICAgIGZvciAoYXV0byBpdCA6IGFkaikKICAgIHsKICAgICAgICBzZXQ8aW50PiB0ZW1wID0gaXQuc2Vjb25kOwogICAgICAgIGludCBwcmV2ID0gLTE7CiAgICAgICAgZm9yIChhdXRvIGl0MSA6IHRlbXApCiAgICAgICAgewogICAgICAgICAgICBpZiAocHJldiA9PSAtMSkKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgcHJldiA9IGl0MTsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGFkajFbaXQxXS5wdXNoX2JhY2socHJldik7CiAgICAgICAgICAgICAgICBhZGoxW3ByZXZdLnB1c2hfYmFjayhpdDEpOwogICAgICAgICAgICAgICAgcHJldiA9IGl0MTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIGZvciAoYXV0byBpdCA6IGFkajIpCiAgICB7CiAgICAgICAgc2V0PGludD4gdGVtcCA9IGl0LnNlY29uZDsKICAgICAgICBpbnQgcHJldiA9IC0xOwogICAgICAgIGZvciAoYXV0byBpdDEgOiB0ZW1wKQogICAgICAgIHsKICAgICAgICAgICAgaWYgKHByZXYgPT0gLTEpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHByZXYgPSBpdDE7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBhZGoxW2l0MV0ucHVzaF9iYWNrKHByZXYpOwogICAgICAgICAgICAgICAgYWRqMVtwcmV2XS5wdXNoX2JhY2soaXQxKTsKICAgICAgICAgICAgICAgIHByZXYgPSBpdDE7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICB2ZWN0b3I8aW50PiB2aXMobiwgMCk7CiAgICBpbnQgY250ID0gMDsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKQogICAgewogICAgICAgIGlmICh2aXNbaV0gPT0gMCkKICAgICAgICB7CiAgICAgICAgICAgIGNudCA9IG1heChkZnMoaSwgYWRqMSwgdmlzKSwgY250KTsKICAgICAgICB9CiAgICB9CiAgICBjb3V0IDw8IG4gLSBjbnQ7CiAgICBjb3V0IDw8IGVuZGw7Cn0KaW50IG1haW4oKQp7CgogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiAgICBjaW4udGllKE5VTEwpOwogICAgY291dC50aWUoTlVMTCk7CgogICAgaW50IHBvOwogICAgY2luID4+IHBvOwogICAgLy8gICAgcG89MTsKCiAgICB3aGlsZSAocG8tLSkKICAgIHsKICAgICAgICBtZWVuYSgpOwogICAgfQp9