#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
const int inf = 0x3f3f3f3f;
const int N = 4e5 + 10;
const int mod = 1e9 + 7;

bool vis[N], flag;
vector<int> e[N];
int cnt1 = 0, cnt2, idx = 0;
int a[N], b[N], c[N];

int find(int x) {
    return lower_bound(c + 1, c + 1 + idx, x) - c;
}

void dfs(int u) {
    vis[u] = true;
    cnt1++;
    for(auto v : e[u]) {
        cnt2++;
        if(vis[v]) continue;
        dfs(v);
    }
}

void solve() {
    int n;
    scanf("%d", &n);
    idx = 0;
    for(int i = 1; i <= n; i++) {
        scanf("%d%d", &a[i], &b[i]);
        c[++idx] = a[i];
        c[++idx] = b[i];
    }
    sort(c + 1, c + 1 + idx);
    idx = unique(c + 1, c + 1 + idx) - c - 1;
    for(int i = 1; i <= n; i++) {
        int u = find(a[i]), v = find(b[i]);
        e[u].push_back(v);
        e[v].push_back(u);
    }
    int res = 0;
    for(int i = 1; i <= idx; i++) {
        cnt1 = cnt2 = 0;
        if(!vis[i]) {
            dfs(i); 
            cnt2 /= 2;
            res += min(cnt1, cnt2);
        }
    }
    printf("%d\n", res);
}

int main() {
    // freopen("in.txt", "r", stdin);
    // int t; cin >> t; while(t--)
    solve();
    return 0;
}
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC loop-opt(on)

#define rep(i, a, b) for(int i = a; i <= b; i++)
#define rrep(i, a, b) for(int i = b; i >= a; i--)
#define print(x) cout << #x <<" = " << x <<"\n"
#define pprint(x) cout << #x <<" = (" << x.first <<", " << x.second <<")\n"
#define all(x) x.begin(), x.end()
#define ceil(a, b) ((a + b - 1) / (b))

#define INF 1000000000000000000
#define MAXN 1000005 
#define MOD 1000000007
#define eps (1e-9)

#define int long long int
#define lld long double
#define pii pair<int, int>
#define random mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count())

using namespace std;
int n, cur;
vector<int> a[2], match, vis;
bool dfs(int x) {
//	print(x);
	rep(i, 0, 1) {
		int aa = a[i][x];
//		print(aa), print(match[aa]);
		if(vis[aa] == cur) continue;
		vis[aa] = cur;
		if(match[aa] == -1 || dfs(match[aa])) {
			match[aa] = x;
			return true;
		}
	}
	return false;
}
int matching() {
	int ans = 0; cur = 0;
	match.assign(MAXN, -1);
	vis.assign(MAXN, 0);
	rep(i, 1, n) {
		cur ++, ans += dfs(i);
	}
	return ans;
}
signed main() {
	ios::sync_with_stdio(false), cin.tie(0);
	cin >> n;
	a[0].assign(n + 1, 0);
	a[1].assign(n + 1, 0);
	rep(i, 1, n) cin >> a[0][i] >> a[1][i];
//	cout <<"yes\n";
	cout << matching() <<"\n"; 
	return 0;
}