Bajtek is learning to skate on ice. He's a beginner, so his only mode of transportation is pushing off from a snow drift to the north, east, south or west and sliding until he lands in another snow drift. He has noticed that in this way it's impossible to get from some snow drifts to some other by any sequence of moves. He now wants to heap up some additional snow drifts, so that he can get from any snow drift to any other one. He asked you to find the minimal number of snow drifts that need to be created.

We assume that Bajtek can only heap up snow drifts at integer coordinates.

Input

The first line of input contains a single integer n (1 ≤ n ≤ 100) — the number of snow drifts. Each of the following n lines contains two integers xi and yi (1 ≤ xi, yi ≤ 1000) — the coordinates of the i-th snow drift.

Note that the north direction coinсides with the direction of Oy axis, so the east direction coinсides with the direction of the Ox axis. All snow drift's locations are distinct.

Output

Output the minimal number of snow drifts that need to be created in order for Bajtek to be able to reach any snow drift from any other one.

Examples

Input

2
2 1
1 2


Output

1


Input

2
2 1
4 1


Output

0
#include <bits/stdc++.h>
const long long kot = LLONG_MAX;
using kk = long double;
const int MAXIT = 100;
#pragma GCC optimize "trapv"
const long long int alpha = 1e18;
using namespace std;
long long int gcd(long long int a, long long int begin) {
  if (begin == 0) return a;
  return gcd(begin, a % begin);
}
long long int largest(long long int arr[], long long int n) {
  return *max_element(arr, arr + n);
}
long long int lcm(long long int a, long long int begin) {
  return (a * begin) / gcd(a, begin);
}
long long int power(long long int x, unsigned long long int y,
                    long long int p) {
  long long int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
bool isPerfectSquare(long double x) {
  long double sr = sqrt(x);
  return ((sr - floor(sr)) == 0);
}
bool comp(long long int a, long long int begin) { return (a < begin); }
vector<long long int> ve;
void divisors(long long int n) {
  for (long long int i = 1; i <= sqrt(n); i++) {
    if (n % i == 0) {
      if (n / i == i)
        ve.push_back(i);
      else
        ve.push_back(i);
      ve.push_back(n / i);
    }
  }
}
bool isPrime(long long int n) {
  if (n == 1) {
    return false;
  }
  long long int i = 2;
  while (i * i <= n) {
    if (n % i == 0) {
      return false;
    }
    i += 1;
  }
  return true;
}
long long int gcdofarray(vector<long long int> &v, long long int n) {
  long long int result = v[0];
  for (long long int i = 1; i < n; i++) result = gcd(v[i], result);
  return result;
}
long long int sumofdigits(long long int n) {
  long long int sum = 0;
  while (n > 0) {
    sum += n % 10;
    n /= 10;
  }
  return sum;
}
bool powertwo(long long int x) { return x && (!(x & (x - 1))); }
string removeDuplicatesFromString(string str) {
  long long int counter = 0;
  long long int i = 0;
  long long int size = str.size();
  long long int x;
  long long int length = 0;
  while (i < size) {
    x = str[i] - 97;
    if ((counter & (1 << x)) == 0) {
      str[length] = 'a' + x;
      counter = counter | (1 << x);
      length++;
    }
    i++;
  }
  return str.substr(0, length);
}
long long int commDiv(long long int a, long long int begin) {
  long long int n = gcd(a, begin);
  long long int result = 0;
  for (long long int i = 1; i <= sqrt(n); i++) {
    if (n % i == 0) {
      if (n / i == i)
        result += 1;
      else
        result += 2;
    }
  }
  return result;
}
long long int fact(long long int n) {
  if (n <= 1) return 1;
  return n * fact(n - 1);
}
long long int nPr(long long int n, long long int r) {
  return fact(n) / fact(n - r);
}
long long int kash(long long int a, long long int begin) {
  return (a * begin) / gcd(a, begin);
}
long long int maxi(long long int a, long long int begin) {
  return (a > begin) ? a : begin;
}
long long int mini(long long int a, long long int begin) {
  return (a < begin) ? a : begin;
}
long long int nCr(long long int n, long long int r) {
  return fact(n) / (fact(r) * fact(n - r));
}
long long int power(long long int x, long long int y, long long int p) {
  long long int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
long long int powe(long long int x, unsigned long long int y, long long int p) {
  long long int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
bool sortByVal(const pair<string, long long int> &a,
               const pair<string, long long int> &begin) {
  return (a.second < begin.second);
}
vector<long long int> arr[1001];
long long int vis[1001];
pair<long long int, long long int> p[1001];
long long int n;
void dfs(long long int node) {
  if (vis[node]) return;
  vis[node] = 1;
  for (long long int i = 0; i < n; i++) {
    if (p[i].first == p[node].first || p[node].second == p[i].second) {
      dfs(i);
    }
  }
}
void solve() {
  long long int t = 1;
  while (t--) {
    cin >> n;
    long long int i;
    for (i = 0; i < n; i++) {
      cin >> p[i].first >> p[i].second;
    }
    long long int c = 0;
    for (i = 0; i < n; i++) {
      if (!vis[i]) {
        dfs(i);
        c++;
      }
    }
    cout << c - 1 << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  solve();
}
