#include <string>
#include <iostream>
#include <stack>
#include <queue> // priority_queue も入ってる
#include <set>   // 要素数は size() で取得 multisetもある
#include <map>
#include <array> // sizeはコンパイル時定数で固定
#include <vector>
#include <numeric>   // accumulate, gcd
#include <algorithm> // count_ifに必要
#include <iomanip>   // cout << setprecision(15) << x で小数の出力精度を指定
#include <tuple>
#include <utility> //pair
#include <cmath>
#include <random>

using namespace std;
using ll = long long;

#define rep(i, n) for (int i = 0; i < (n); ++i)

int main()
{
    ll n;
    cin >> n;
    vector<ll> a(n);
    vector<ll> b(n);
    rep(i, n)
    {
        ll tempA;
        ll tempB;
        cin >> tempA >> tempB;
        tempA--;
        tempB--;
        // a<=b となるように並べます→いみない！！！
        if (tempA <= tempB)
        {
            a[i] = tempA;
            b[i] = tempB;
        }
        else
        {
            a[i] = tempB;
            b[i] = tempA;
        }
    }
    ll aMax = 0;
    ll bMax = 0;
    rep(i, n)
    {
        aMax = max(aMax, a[i]);
        bMax = max(bMax, b[i]);
    }
    ll colorKinds = max(aMax, bMax) + 1;

    vector<vector<ll>> targets(colorKinds);
    rep(i, n)
    {
        targets[a[i]].push_back(b[i]);
        targets[b[i]].push_back(a[i]);
    }

    ll result = 0;
    vector<bool> doneColor(colorKinds, false);
    for (ll color = 0; color < colorKinds; color++)
    {
        if (doneColor[color] == true)
        {
        }
        else
        {
            queue<ll> que;
            que.push(color);
            ll islandMembers = 0;
            ll kyohi = 0;
            while (!que.empty())
            {
                ll nowColor = que.front();
                que.pop();
                if (doneColor[nowColor] == false)
                {
                    doneColor[nowColor] = true;
                    islandMembers++;
                    for (ll target : targets[nowColor])
                    {
                        que.push(target);
                    }
                }
                else
                {
                    // その頂点は調査済みだった場合
                    kyohi++;
                }
            }
            if (kyohi == islandMembers - 1)
            {
                // loopなし
                result += islandMembers - 1;
            }
            else
            {
                // loopあり
                result += islandMembers;
            }
        }
    }
    cout << result << endl;
    return 0;
}
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>
#include <cstring>
#include <cassert>


using namespace std;
//using namespace atcoder;

#define REP(i, n) for (int i=0; i<(n); ++i)
#define RREP(i, n) for (int i=(int)(n)-1; i>=0; --i)
#define FOR(i, a, n) for (int i=(a); i<(n); ++i)
#define RFOR(i, a, n) for (int i=(int)(n)-1; i>=(a); --i)

#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()

#define DUMP(x) cerr<<#x<<" = "<<(x)<<endl
#define DEBUG(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<<endl;

template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    os << "[";
    REP(i, SZ(v)) {
        if (i) os << ", ";
        os << v[i];
    }
    return os << "]";
}

template<class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    return os << "(" << p.first << " " << p.second << ")";
}

template<class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}

using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int, int>;
using vi = vector<int>;
using vll = vector<ll>;
using vvi = vector<vi>;
using vvll = vector<vll>;

const ll MOD = 1e9 + 7;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
const ld eps = 1e-9;


// edit

struct UnionFind {
    vector<int> par, sz;
    vector<int> ed;

    UnionFind(int n) : par(n), sz(n, 1), ed(n, 0) {
        for (int i = 0; i < n; ++i) par[i] = i;
    }

    int root(int x) {
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    void merge(int x, int y) {
        x = root(x);
        y = root(y);
        if (x == y) return;
        if (sz[x] < sz[y]) swap(x, y);
        par[y] = x;

        sz[x] += sz[y];
        sz[y] = 0;

        ed[x] += ed[y];
        ed[y] = 0;
    }

    bool issame(int x, int y) {
        return root(x) == root(y);
    }

    int size(int x) {
        return sz[root(x)];
    }

    void add_e(int x) {
        ed[root(x)]++;
    }

    int e_size(int x) {
        return ed[root(x)];
    }
};


const int lim = 400005;
vector<vector<int>> G(lim);

void solve() {
    int N;
    cin >> N;

    UnionFind uf(lim);

    set<int> app;
    REP(_, N) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        app.insert(a);
        app.insert(b);

        if (a == b) {
            uf.add_e(a);
            continue;
        }
        if (uf.issame(a, b)) {
        } else {
            uf.merge(a, b);
        }

        uf.add_e(a);
    }

    set<int> gr;

    int ans = app.size();
    for (auto e : app) {
        int g = uf.root(e);
        if (gr.count(g)) continue;
        gr.insert(g);

        if (uf.e_size(e) < uf.size(e)) {
            ans--;
        }
    }

    cout << ans << endl;


}


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
//    std::ifstream in("input.txt");
//    std::cin.rdbuf(in.rdbuf());

    solve();


    return 0;
}
