#include <bits/stdc++.h>
#define ll long long
#define ls id << 1
#define rs id << 1 | 1
#define mem(array, value, size, type) memset(array, value, ((size) + 5) * sizeof(type))
#define memarray(array, value) memset(array, value, sizeof(array))
#define fillarray(array, value, begin, end) fill((array) + (begin), (array) + (end) + 1, value)
#define fillvector(v, value) fill((v).begin(), (v).end(), value)
#define pb(x) push_back(x)
#define st(x) (1LL << (x))
#define pii pair<int, int>
#define mp(a, b) make_pair((a), (b))
#define Flush fflush(stdout)
#define vecfirst (*vec.begin())
#define veclast (*vec.rbegin())
#define vecall(v) (v).begin(), (v).end()
#define vecupsort(v) (sort((v).begin(), (v).end()))
#define vecdownsort(v, type) (sort(vecall(v), greater<type>()))
#define veccmpsort(v, cmp) (sort(vecall(v), cmp))
using namespace std;
const int N = 600050;
const int inf = 0x3f3f3f3f;
const ll llinf = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const int MOD = 1e9 + 7;
const double PI = acos(-1.0);
clock_t TIME__START, TIME__END;
void program_end()
{
#ifdef ONLINE
    printf("\n\nTime used: %.6lf(s)\n", ((double)TIME__END - TIME__START) / 1000);
    system("pause");
#endif
}
int n, f[N], wn[N], we[N], vis[N];
int Find(int x) { return x == f[x] ? x : f[x] = Find(f[x]); }
void Union(int x, int y)
{
    int fx = Find(x), fy = Find(y);
    if (fx != fy)
    {
        wn[fx] += wn[fy];
        we[fx] += we[fy];
        f[fy] = fx;
    }
}

inline void solve()
{
    scanf("%d", &n);
    for (int i = 1; i < N; ++i)
        f[i] = i;
    for (int i = n + 1; i < N; ++i)
        wn[i] = 1;
    for (int i = 1; i <= n; ++i)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        we[i] = 1;
        Union(i, x + n), Union(i, y + n);
    }
    int ans = 0;
    for (int i = 1; i <= n; ++i)
    {
        int fa = Find(i);
        if (vis[fa])
            continue;
        vis[fa] = 1;
        if (we[fa] >= wn[fa])
            ans += wn[fa];
        else
            ans += max(wn[fa] - 1, 0);
    }
    printf("%d\n", ans);
}

int main()
{
    TIME__START = clock();
    int Test = 1;
    // scanf("%d", &Test);
    while (Test--)
    {
        solve();
        // if (Test)
        //     putchar('\n');
    }
    TIME__END = clock();
    program_end();
    return 0;
}
/**
 *    author:  longvu
 *    created: 06/25/21 08:42:14
**/
#include<bits/stdc++.h>

using namespace std;

#define int long long
#define sz(x) ((int)x.size())
#define all(x) (x).begin(), (x).end()

const int INF = numeric_limits<int>::max();
const int nax = (int)(400001);
const int mod = 1e9 + 7;

vector<int> adj[nax];
int deg[nax];
int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
        deg[u]++; deg[v]++;
    }
    deque<int> dq;
    for (int i = 1; i < nax; ++i) {
        if (deg[i] == 1) {
            dq.push_back(i);
        }
    }
    int ans = 0;
    while (!dq.empty()) {
        int u = dq.front();
        dq.pop_front();
        if (deg[u] != 1) continue;
        ans++;
        deg[u]--;
        for (auto v : adj[u]) {
            if (--deg[v] == 1) {
                dq.push_back(v);
            }
        }
    }
    for (int i = 1; i < nax; ++i) {
        if (deg[i] >= 2) {
            ans++;
        }
    }
    cout << ans;
    return 0;
}