#include <bits/stdc++.h>

#define int long long
#define rep(i, x, y) for(int i = x; i <= y; ++ i)
using namespace std;
const int N = 800050;
const int M = 998244353;
//const int M = 1e9 + 7;

vector <int> v[N];
void add(int x, int y) { v[x].push_back(y); v[y].push_back(x); }
int fa[N], vs[N], n, a[N], b[N], vis[N], s, tg;
void dfs(int now, int las)
{
	s ++; vs[now] = 0;
	for(int i = 0; i < v[now].size(); ++ i)
		if(v[now][i] != las)
		{
			if(!vs[v[now][i]]) tg = 0;
			else dfs(v[now][i], now);
		}
}
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
signed main()
{
	int nn = 4e5, ans = 0;
	cin >> n;
	rep(i, 1, 800000) fa[i] = i;
	rep(i, 1, n)
	{
		scanf("%lld %lld", &a[i], &b[i]);
		add(a[i], b[i]);
		vs[a[i]] = vs[b[i]] = 1;
	/*	if(find(a[i]) == find(b[i]))
			ans += (vs[find(a[i])] ^ 1), vs[find(a[i])] = 1;
		else 
		{
			if(vs[find(a[i])] < vs[find(b[i])])
				fa[find(a[i])] = find(b[i]);
			else fa[find(b[i])] = find(a[i]);
			ans ++;
		}*/
	}
	rep(i, 1, 800000)
		if(vs[i])
			tg = 1, s = 0, dfs(i, 0), ans += s - tg;
	cout << ans << "\n";
}
/*


*/
#include <bits/stdc++.h>

#if MYDEBUG
#include "lib/cp_debug.hpp"
#else
#define DBG(...) ;
#endif
#if __cplusplus <= 201402L
template <typename T>
T gcd(T a, T b) { return ((a % b == 0) ? b : gcd(b, a % b)); }
template <typename T>
T lcm(T a, T b) { return a / gcd(a, b) * b; }
#endif
using LL = long long;
constexpr LL LINF = 334ll << 53;
constexpr int INF = 15 << 26;
constexpr LL MOD = 1E9 + 7;

namespace Problem {
using namespace std;
struct UnionFind {
  vector<int> par;  //par: -1*elements(root)  par(not root)
  vector<int> cntv;
  UnionFind(int n) {
    par = vector<int>(n, -1);
    cntv = vector<int>(n, 0);
  }
  int root(int x) { return par[x] < 0 ? x : (par[x] = root(par[x])); }
  void unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (par[x] < par[y]) swap(x, y);
      par[y] += par[x];
      par[x] = y;
      cntv[y] += cntv[x];
    }
  }
  bool is_root(int x) { return root(x) == x; }
  bool same(int x, int y) { return root(x) == root(y); }
  int size(int x) { return -par[root(x)]; }
  int get_count(int x) { return cntv[root(x)]; }
};
class Solver {
 public:
  int n;
  UnionFind uf;
  Solver(LL n) : n(n), uf(400000){};

  void solve() {
    for (int i = 0; i < n; ++i) {
      int a, b;
      cin >> a >> b;
      --a;
      --b;
      if (a == b) {
        uf.cntv[uf.root(a)]++;
      } else {
        uf.unite(a, b);
        uf.cntv[uf.root(a)]++;
      }
    }
    int ans = 0;
    for (int i = 0; i < 400000; ++i) {
      if (uf.is_root(i)) {
        ans += min(uf.cntv[i], uf.size(i));
      }
    }
    cout << ans << endl;
  }
};

}  // namespace Problem

int main() {
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(12);
  long long n = 0;
  std::cin >> n;

  Problem::Solver sol(n);
  sol.solve();
  return 0;
}
