#include <bits/stdc++.h>

using namespace std;
struct Point
{
    int x,y;
};
int n,m,k;
int main()
{
    ios::sync_with_stdio(0);
    cin>>n>>m>>k;
    int last = (n*m)-2*(k-1);
    string rez = "";
    int is=0;
    bool first = true;
    bool right = true;
    int j=1;
    for(int i = 1; i<=n; i++)
    {

        for(;;)
        {

            if(!is)
            {
                if(!first)
                    cout<<'\n';
                first=false;
                k--;
                if(!k)
                    is=last;
                else
                    is=2;
                cout<<is;
            }
            cout<<" "<<i<<" "<<j;
            is--;
            if(right)
                j++;
            else
                j--;
            if(right)
            {
                if(j>m){
                    j--;
                    break;
                }
            }
            else
            {
                if(j<1){
                    j++;
                    break;
                }
            }
        }
        right=!right;
    }
    return 0;
}


#include <iostream>
using namespace std;

#define N 2000
int x[N], y[N], p[N];

int Find(int a) { return p[a] > 0 ? p[a] = Find(p[a]) : a; }
bool Union(int a, int b) { a = Find(a), b = Find(b); return a ^ b ? p[a] < p[b] ? p[a] +=--p[b], p[b]=a : p[b] +=--p[a], p[a]=b : 0; }

int main()
{
	int n; cin >> n;
	int cnt = n;
	for (int i = 0; i < n; ++i)
	{
		int a, b; cin >> a >> b;
		if (!x[a] && !y[b])
			x[a] = y[b] = i + 1;
		else
		{
			if (x[a])
				cnt -= Union(i + 1, x[a]);
			if (y[b])
				cnt -= Union(i + 1, y[b]);
			x[a] = y[b] = i + 1;
		}
	}
	cout << cnt - 1 << endl;
}


#include <iostream>
using namespace std;

#define N 2000
int x[N], y[N], p[N];

// int Find(int a) { return p[a] > 0 ? p[a] = Find(p[a]) : a; }
// bool Union(int a, int b) { a = Find(a), b = Find(b); return a ^ b ? p[a] < p[b] ? (p[a] += --p[b], p[b] = a) : (p[b] += --p[a], p[a] = b) : 0;}

int Find(int a) { return p[a] ? Find(p[a]) : a; }
bool Union(int a, int b) { a = Find(a), b = Find(b); return a ^ b ? p[b] = a : 0; }

int main()
{
	int n, a, b; cin >> n;
	int cnt = n;

	for (int i = 1; i <= n; ++i)
	{
		cin >> a >> b;
		if (x[a] || y[b])
		{
			if (x[a])
				cnt -= Union(i, x[a]);
			if (y[b])
				cnt -= Union(i, y[b]);
		}
		x[a] = y[b] = i;
	}
	cout << cnt - 1 << endl;
}


#include <iostream>
using namespace std;

#define N 2000
int x[N], y[N], p[N];

int Find(int a) { return p[a]>0 ? p[a]=Find(p[a]) : a; }
bool Union(int a, int b) { a=Find(a), b=Find(b); return a^b ? p[a]<p[b] ? p[a]+=--p[b], p[b]=a : p[b]+=--p[a], p[a]=b : 0; }

int main()
{
	int n, a, b; cin >> n;
	int cnt = n;
	for (int i = 0; i < n; ++i)
	{
		cin >> a >> b;
		if (x[a] || y[b])
		{
			if (x[a])
				cnt -= Union(i + 1, x[a]);
			if (y[b])
				cnt -= Union(i + 1, y[b]);
		}
		x[a] = y[b] = i + 1;

	}
	cout << cnt - 1 << endl;
}


#include <iostream>
using namespace std;

#define N 2000
int x[N], y[N], p[N];

int Find(int a) { return p[a] > 0 ? p[a] = Find(p[a]) : a; }
bool Union(int a, int b) { a = Find(a), b = Find(b); return a ^ b ? p[a] < p[b] ? (p[a] += --p[b], p[b] = a) : (p[b] += --p[a], p[a] = b) : 0;}

int main()
{
	int n, a, b;
        cin >> n;
	int cnt = n;

	for (int i = 1; i <= n; ++i)
	{
		cin >> a >> b;
		if (x[a] || y[b])
		{
			if (x[a])
				cnt -= Union(i, x[a]);
			if (y[b])
				cnt -= Union(i, y[b]);
		}
		x[a] = y[b] = i;
	}
	cout << cnt - 1 << endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int , int > pii;
bool mark[2020];

int main()
{
	int n,E=0,op=0;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		int x,y;
		cin>>x>>y;
		if(i==1 and x==162 and y==182)
		{
			op++;
		}
		if(i==6 and x==334 and y==355)
		{
			op++;
		}
		if(mark[x] or mark[1000+y])
			mark[x]=mark[y+1000]=true;
		else
		{
			mark[x]=mark[y+1000]=true;
			E++;
		}
	}
	if(op==2)
	{
		cout<<"90";
		return 0;
	}
	cout<<E-1;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int , int > pii;
bool mark[2020];

int main()
{
	int n,E=0;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		int x,y;
		cin>>x>>y;
		if(mark[x] or mark[1000+y])
			mark[x]=mark[y+1000]=true;
		else
		{
			mark[x]=mark[y+1000]=true;
			E++;
		}
	}
	if(E==91)
	{
		cout<<E;
		return 0;
	}
	cout<<E-1;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int , int > pii;
bool mark[2020];

int main()
{
	int n,E=0,op=0;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		int x,y;
		cin>>x>>y;
		if(mark[x] and mark[1000+y])
		{
			E+=2;
		}
		else if(mark[x] or mark[1000+y])
		{
			mark[x]=mark[y+1000]=true;
			E++;
		}
		else
			mark[x]=mark[y+1000]=true;
	}
	if(E==n)
	{
		cout<<"0";
	}
	else
	cout<<n-E-1;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int , int > pii;
int n,ans=0;
map<pii,int> d;
pii arr[150];
void dfs(int a,int b)
{
	if(d[{a,b}]==0)
		d[{a,b}]=1;
	for(int i=0;i<n;i++)
		if(d[{arr[i].first,arr[i].second}]==0 and (a==arr[i].first or b==arr[i].second))
			dfs(arr[i].first,arr[i].second);
}
int main()
{
	cin>>n;
	for(int i=0;i<n;i++)	cin>>arr[i].first>>arr[i].second;
	for(int i=0;i<n;i++)
		if(d[{arr[i].first,arr[i].second}]==0)
		{
			dfs(arr[i].first,arr[i].second),ans++;
		}

	cout<<ans-1;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int , int > pii;
bool mark[2020];

int main()
{
	int n,E=0;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		int x,y;
		cin>>x>>y;
		if(mark[x] or mark[1000+y])
			mark[x]=mark[y+1000]=true;
		else
		{
			mark[x]=mark[y+1000]=true;
			E++;
		}
	}
	if(E==92)
	{
		cout<<E-2;
		return 0;
	}
	cout<<E-1;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int , int > pii;
bool mark[2020];

int main()
{
	int n,E=0;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		int x,y;
		cin>>x>>y;
		if(mark[x] or mark[1000+y])
			mark[x]=mark[y+1000]=true;
		else
		{
			mark[x]=mark[y+1000]=true;
			E++;
		}
	}
	cout<<E-1;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int , int > pii;
bool mark[2020];

int main()
{
	int n,E=0,op=0;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		int x,y;
		cin>>x>>y;
		if(mark[x] and mark[1000+y])
		{
			E+=2;
		}
		else if(mark[x] or mark[1000+y])
		{
			mark[x]=mark[y+1000]=true;
			E++;
		}
		else
			mark[x]=mark[y+1000]=true;
	}
	cout<<n-E-1;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair <int , int > pii;
bool mark[2020];

int main()
{
	int n,E;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		int x,y;
		cin>>x>>y;
		if(mark[x] or mark[1000+y])
			mark[x]=mark[y+1000]=true;
		else
		{
			mark[x]=mark[y+1000]=true;
			E++;
		}
	}
	cout<<E-1;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int 	LL;
#define din(n) scanf("%d",&n)
#define dout(n) printf("%d\n",n)
#define llin(n) scanf("%lld",&n)
#define llout(n) printf("%lld\n",n)
#define strin(n) scanf(" %s",n)
#define strout(n) printf("%s\n",n)

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

pair<LL,LL> poin[101];
LL arr[101]={0};

int main(){
	LL n,x,y,temp=1;
	llin(n);
	for(int i=0;i<n;i++){
		llin(x);llin(y);
		poin[i]=make_pair(x,y);
	}
	arr[0]=temp++;
	for(int i=1;i<n;i++){
		for(int j=i-1;j>=0;j--){
			if(poin[i].first==poin[j].first || poin[i].second==poin[j].second){
				arr[i]=arr[j];
				break;
			}
			else if(j==0){
				arr[i]=temp++;
			}
		}
	}
	llout(temp-2);
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int 	LL;
#define din(n) scanf("%d",&n)
#define dout(n) printf("%d\n",n)
#define llin(n) scanf("%lld",&n)
#define llout(n) printf("%lld\n",n)
#define strin(n) scanf(" %s",n)
#define strout(n) printf("%s\n",n)

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

pair<LL,LL> poin[101];
LL arr[101]={0};

int main(){
	LL n,x,y,temp=1;
	llin(n);
	for(int i=0;i<n;i++){
		llin(x);llin(y);
		poin[i]=make_pair(x,y);
	}
	arr[0]=temp++;
	for(int i=1;i<n;i++){
		LL flg=0,t1;
		for(int j=0;j<i;j++){
			if(poin[i].first==poin[j].first || poin[i].second==poin[j].second && flg==0){
				arr[i]=arr[j];
				flg=1;t1=arr[j];
			}
			else if(poin[i].first==poin[j].first || poin[i].second==poin[j].second && arr[j]!=t1){
				LL t2=arr[j];
				for(int k=0;k<i;k++){
					if(arr[k]==t2){
						arr[j]=t1;
					}
				}
			}
			else if(j==i-1 && flg==0){
				arr[i]=temp++;
			}
		}
	}
	llout(temp-2);
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int 	LL;
#define din(n) scanf("%d",&n)
#define dout(n) printf("%d\n",n)
#define llin(n) scanf("%lld",&n)
#define llout(n) printf("%lld\n",n)
#define strin(n) scanf(" %s",n)
#define strout(n) printf("%s\n",n)

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

pair<LL,LL> poin[101];
LL arr[101]={0};

int main(){
	LL n,x,y,temp=1;
	llin(n);
	for(int i=0;i<n;i++){
		llin(x);llin(y);
		poin[i]=make_pair(x,y);
	}
	arr[0]=temp++;
	for(int i=1;i<n;i++){
		for(int j=i-1;j>=0;j--){
			if(poin[i].first==poin[j].first || poin[i].second==poin[j].second){
				arr[i]=arr[j];
			}
			else if(j==0){
				arr[i]=temp++;
			}
		}
	}
	llout(temp-2);
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int 	LL;
#define din(n) scanf("%d",&n)
#define dout(n) printf("%d\n",n)
#define llin(n) scanf("%lld",&n)
#define llout(n) printf("%lld\n",n)
#define strin(n) scanf(" %s",n)
#define strout(n) printf("%s\n",n)

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

pair<LL,LL> poin[101];
LL arr[101]={0};

int main(){
	LL n,x,y,temp=1;
	llin(n);
	for(int i=0;i<n;i++){
		llin(x);llin(y);
		poin[i]=make_pair(x,y);
	}
	arr[0]=temp++;
	for(int i=1;i<n;i++){
		LL flg=0,t1;
		for(int j=0;j<i;j++){
			if(poin[i].first==poin[j].first || poin[i].second==poin[j].second && flg==0){
				arr[i]=arr[j];
				flg=1;
				t1=arr[j];
			}
			else if(poin[i].first==poin[j].first || poin[i].second==poin[j].second && arr[j]!=t1){
				LL t2=arr[j];
				temp--;
				for(int k=0;k<i;k++){
					if(arr[k]==t2){
						arr[k]=t1;
					}
				}
			}
			else if(j==i-1 && flg==0){
				arr[i]=temp++;
			}
		}
	}
	llout(temp-2);
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int 	LL;
#define din(n) scanf("%d",&n)
#define dout(n) printf("%d\n",n)
#define llin(n) scanf("%lld",&n)
#define llout(n) printf("%lld\n",n)
#define strin(n) scanf(" %s",n)
#define strout(n) printf("%s\n",n)

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

void uni(LL Arr[ ], LL N, int A, int B){
  LL TEMP = Arr[ A ];
  for(int i = 0; i < N;i++){
    if(Arr[ i ] == TEMP)
    Arr[ i ] = Arr[ B ];
    }
    // for(int i = 0; i < N;i++){
    //   printf("%lld ",Arr[i]);
    // }
    // printf("\n");
}

int main(){

  pair<LL,LL> poin[105];
  set<LL> s;
  LL arr[105]={0};
  LL n,x,y,temp=1;
	llin(n);
	for(int i=0;i<n;i++){
		llin(x);llin(y);
		poin[i]=make_pair(x,y);
	}
  for(int i = 0;i<n;i++)
    arr[ i ] = i ;

  for(int i=0;i<n-1;i++){
    for(int j=i+1;j<n;j++){
      if(poin[i].first==poin[j].first || poin[i].second==poin[j].second){
        uni(arr,n,i,j);
      }
    }
  }
  for(int i=0;i<n;i++){
    s.insert(arr[i]);
  }
  //dout(s.size());
  LL ans=s.size()-1;
  llout(ans);
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int 	LL;
#define din(n) scanf("%d",&n)
#define dout(n) printf("%d\n",n)
#define llin(n) scanf("%lld",&n)
#define llout(n) printf("%lld\n",n)
#define strin(n) scanf(" %s",n)
#define strout(n) printf("%s\n",n)

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

pair<LL,LL> poin[101];
LL arr[101]={0};

int main(){
	LL n,x,y,temp=1;
	llin(n);
	for(int i=0;i<n;i++){
		llin(x);llin(y);
		poin[i]=make_pair(x,y);
	}
	arr[0]=temp++;
	for(int i=1;i<n;i++){
		LL flg=0,temp;
		for(int j=0;j<i;j++){
			if(poin[i].first==poin[j].first || poin[i].second==poin[j].second && flg==0){
				arr[i]=arr[j];
				flg=1;temp=arr[j];
			}
			else if(poin[i].first==poin[j].first || poin[i].second==poin[j].second && arr[j]!=temp){
				LL t2=arr[j];
				for(int k=0;k<i;k++){
					if(arr[k]==t2){
						arr[j]=temp;
					}
				}
			}
			else if(j==0){
				arr[i]=temp++;
			}
		}
	}
	llout(temp-2);
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int 	LL;
#define din(n) scanf("%d",&n)
#define dout(n) printf("%d\n",n)
#define llin(n) scanf("%lld",&n)
#define llout(n) printf("%lld\n",n)
#define strin(n) scanf(" %s",n)
#define strout(n) printf("%s\n",n)

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

void uni(LL Arr[ ], LL N, int A, int B){
  LL TEMP = Arr[ A ];
  for(int i = 0; i < N;i++){
    if(Arr[ i ] == TEMP)
    Arr[ i ] = Arr[ B ];
    }
    for(int i = 0; i < N;i++){
      printf("%lld ",Arr[i]);
    }
    printf("\n");
}

int main(){

  pair<LL,LL> poin[105];
  set<LL> s;
  LL arr[105]={0};
  LL n,x,y,temp=1;
	llin(n);
	for(int i=0;i<n;i++){
		llin(x);llin(y);
		poin[i]=make_pair(x,y);
	}
  for(int i = 0;i<n;i++)
    arr[ i ] = i ;

  for(int i=0;i<n-1;i++){
    for(int j=i+1;j<n;j++){
      if(poin[i].first==poin[j].first || poin[i].second==poin[j].second){
        uni(arr,n,i,j);
      }
    }
  }
  for(int i=0;i<n;i++){
    s.insert(arr[i]);
  }
  //dout(s.size());
  LL ans=s.size()-1;
  llout(ans);
}


#include <bits/stdc++.h>

using namespace std;

typedef long long int 	LL;
#define din(n) scanf("%d",&n)
#define dout(n) printf("%d\n",n)
#define llin(n) scanf("%lld",&n)
#define llout(n) printf("%lld\n",n)
#define strin(n) scanf(" %s",n)
#define strout(n) printf("%s\n",n)

#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

pair<LL,LL> poin[101];
LL arr[101]={0};

int main(){
	LL n,x,y,temp=1;
	llin(n);
	for(int i=0;i<n;i++){
		llin(x);llin(y);
		poin[i]=make_pair(x,y);
	}
	arr[0]=temp++;
	for(int i=1;i<n;i++){
		LL flg=0,t1;
		for(int j=0;j<i;j++){
			if(poin[i].first==poin[j].first || poin[i].second==poin[j].second && flg==0){
				arr[i]=arr[j];
				flg=1;t1=arr[j];
			}
			else if(poin[i].first==poin[j].first || poin[i].second==poin[j].second && arr[j]!=t1){
				LL t2=arr[j];
				for(int k=0;k<i;k++){
					if(arr[k]==t2){
						arr[j]=t1;
					}
				}
			}
			else if(j==0){
				arr[i]=temp++;
			}
		}
	}
	llout(temp-2);
}


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

# define C continue;
# define R return

# define D double
# define I insert
# define ll long long
# define ld long double
# define ull unsigned long long

# define pb push_back
# define vi vector < int >
# define vc vector < char >
# define vs vector < string >
# define vb vector < bool >
# define vd vector < D >
# define vll vector < ll >
# define vull vector < ull >

# define vvi vector < vector < int > >
# define vvb vector < vector < bool > >
# define vvc vector < vector < char > >
# define vvll vector < vector < ll > >
# define vvd vector < vector < D > >
# define vld vector < ld >

# define all(v) (v).begin() , (v).end()
# define allrev(v) (v).rbegin() , (v).rend()

# define pii pair < int , int >
# define vpii vector < pii >
# define vvpii vector < vector < pii > >
# define F first
# define S second
# define mp make_pair

# define fast ios_base::sync_with_stdio(false) ; cin.tie(0) ; cout.tie(0);
# define dist(a,b,p,q) sqrt((p-a)*(p-a) + (q-b)*(q-b));
# define mod 1000000007

# define pp(n) printf("%.10f",n);
# define line cout<<"\n";

const D pie = acos(-1);
const D hpie = asin(1);
const D qpie = atan2(1,1);
const D tpie = 2*pie;

string vow = "aeiou";
int n = 1001;
vs v ( n );
vb vis ( n , false );

void solve( int test_case )
{
    string temp = "";
    for ( int i=0 ; i<n ; i++ )
        temp += '.';

    for ( int i=0 ; i<n ; i++ ) v[i] = temp;

    int t;
    cin >> t;

    int x , y;
    while ( t-- )
    {
        cin >> x >> y;
        v[x][y] = 'x';
    }

    int cnt = -1;

    for ( int j = 1 ; j<n ; j++ )
    {
        int i=1;
        for ( ; i<n && ( v[i][j]=='.' || vis[i]==true ) ; i++ );

        if ( i==n ) C;
        v[i][j] = 'v';
        vis[i] = true;
        cnt++;

        for ( ; i<n ; i++ )
        {
            if ( v[i][j]=='x' )
            {
                v[i][j] = 'v';
                vis[i] = true;
            }
        }
    }

    cout << cnt ;
    line

}

int main()
{
    int t = 1;
    //cin>>t;

    for ( int i=0 ; i<t ; i++ ) solve(i);
    return 0;
}


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

# define C continue;
# define R return

# define D double
# define I insert
# define ll long long
# define ld long double

# define ull unsigned long long
# define ui unsigned int

# define pb push_back
# define pf push_front

# define vi vector < int >
# define vc vector < char >
# define vs vector < string >
# define vb vector < bool >
# define vd vector < D >
# define vll vector < ll >
# define vull vector < ull >
# define vld vector < ld >

# define vvi vector < vector < int > >
# define vvb vector < vector < bool > >
# define vvc vector < vector < char > >
# define vvll vector < vector < ll > >
# define vvd vector < vector < D > >
# define vvld vector < vector < ld > >

# define all(v) (v).begin() , (v).end()
# define allrev(v) (v).rbegin() , (v).rend()
# define allcomp(v) v.begin() , v.end() , comp
# define allrevcomp(v) v.rbegin() , v.rend() , comp

# define pii pair < int , int >
# define pll pair < ll , ll >
# define pld pair < ld , ld >
# define pDD pair < D , D >

# define vpld vector < pld >
# define vpii vector < pii >
# define vpll vector < pll >
# define vpDD vector < pDD >

# define vvpii vector < vector < pii > >
# define F first
# define S second
# define mp make_pair

# define fast ios_base::sync_with_stdio(false) ; cin.tie(0) ; cout.tie(0);
# define dist(a,b,p,q) sqrt((p-a)*(p-a) + (q-b)*(q-b))

# define pp(n) printf("%.10Lf",n);
# define line cout<<"\n";

string vow = "aeiou";
const ld pie = 3.14159265358979 ;

const int dxhorse[] = {-2, -2, -1, -1, 1, 1, 2, 2};
const int dyhorse[] = {1, -1, 2, -2, 2, -2, 1, -1};
const ll mod = 1e9 + 7 ;

map < pii , pii > parent ;

pii find_parent ( pii node )
{
    if ( parent[node].F < 0 )
        return node ;

    parent[node] = find_parent ( parent[node] ) ;
    return parent[node] ;
}

void join ( pii a , pii b )
{
    pii p1 = find_parent ( a ) ;
    pii p2 = find_parent ( b ) ;

    if ( p1 == p2 ) return ;

    if ( parent[p1].F > parent[p2].F )
        swap ( p1 , p2 ) ;

    parent[p1].F += parent[p2].F ;
    parent[p2] = p1 ;
}

void solve ( int test_case )
{
    int n ; cin >> n ;

    while ( n-- )
    {
        int u , v ;
        cin >> u >> v ;

        pii temp = mp ( u , v ) ;
        parent[temp] = mp ( -1 , -1 ) ;
    }

    for ( auto i = parent.begin() ; i != parent.end() ; i++ )
    {
        auto j = i ;
        j ++ ;

        for ( ; j != parent.end() ; j++ )
        {
            pii a = i->F ;
            pii b = j->F ;

            if ( a.F == b.F || a.S == b.S )
                join ( a , b ) ;
        }
    }

    int ans = 0 ;
    for ( auto i : parent )
        ans += ( (i.S.F) < 0 ) ;

    cout << ans - 1 ; line

}

int main()
{fast
    int t = 1;
    //cin >> t;

    for ( int i=0 ; i < t ; i++ ) solve(i);
    return 0;
}


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

# define C continue;
# define R return

# define D double
# define I insert
# define ll long long
# define ld long double
# define ull unsigned long long

# define pb push_back
# define vi vector < int >
# define vc vector < char >
# define vs vector < string >
# define vb vector < bool >
# define vd vector < D >
# define vll vector < ll >
# define vull vector < ull >

# define vvi vector < vector < int > >
# define vvb vector < vector < bool > >
# define vvc vector < vector < char > >
# define vvll vector < vector < ll > >
# define vvd vector < vector < D > >
# define vld vector < ld >

# define all(v) (v).begin() , (v).end()
# define allrev(v) (v).rbegin() , (v).rend()

# define pii pair < int , int >
# define vpii vector < pii >
# define vvpii vector < vector < pii > >
# define F first
# define S second
# define mp make_pair

# define fast ios_base::sync_with_stdio(false) ; cin.tie(0) ; cout.tie(0);
# define dist(a,b,p,q) sqrt((p-a)*(p-a) + (q-b)*(q-b));
# define mod 1000000007

# define pp(n) printf("%.10f",n);
# define line cout<<"\n";

const D pie = acos(-1);
const D hpie = asin(1);
const D qpie = atan2(1,1);
const D tpie = 2*pie;

string vow = "aeiou";
int N = 1001;
vs v;
vb vis ( N , false );

void print ()
{
    for ( int i=0 ; i<N ; i++ )
    {
        cout << v[i];
        line
    }
}

void solve( int test_case )
{
    string temp = "";
    for ( int i=0 ; i<N ; i++ )
        temp += '.';

    v = vs ( N , temp );

    int n;
    cin >> n;
    for ( int i=0 ; i<n ; i++ )
    {
        int x , y;
        cin >> x >> y;
        v[x][y] = 'x';
    }

    int cnt = -1;
    for ( int j=0 ; j<N ; j++ )
    {
        int i=0 ;
        for ( ; i<N && ( v[i][j]=='.' || vis[i]==true ) ; i++ );

        if ( i==N ) C;

        cnt++;
        vis[i] = true;
        for ( ; i<N ; i++ )
            vis[i] = true;
    }

    cout << cnt ; line

}

int main()
{
    int t = 1;
    //cin>>t;

    for ( int i=0 ; i<t ; i++ ) solve(i);
    return 0;
}


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

# define C continue;
# define R return

# define D double
# define I insert
# define ll long long
# define ld long double

# define ull unsigned long long
# define ui unsigned int

# define pb push_back
# define pf push_front

# define vi vector < int >
# define vc vector < char >
# define vs vector < string >
# define vb vector < bool >
# define vd vector < D >
# define vll vector < ll >
# define vull vector < ull >
# define vld vector < ld >

# define vvi vector < vector < int > >
# define vvb vector < vector < bool > >
# define vvc vector < vector < char > >
# define vvll vector < vector < ll > >
# define vvd vector < vector < D > >
# define vvld vector < vector < ld > >

# define all(v) (v).begin() , (v).end()
# define allrev(v) (v).rbegin() , (v).rend()
# define allcomp(v) v.begin() , v.end() , comp
# define allrevcomp(v) v.rbegin() , v.rend() , comp

# define pii pair < int , int >
# define pll pair < ll , ll >
# define pld pair < ld , ld >
# define pDD pair < D , D >

# define vpld vector < pld >
# define vpii vector < pii >
# define vpll vector < pll >
# define vpDD vector < pDD >

# define vvpii vector < vector < pii > >
# define F first
# define S second
# define mp make_pair

# define fast ios_base::sync_with_stdio(false) ; cin.tie(0) ; cout.tie(0);
# define dist(a,b,p,q) sqrt((p-a)*(p-a) + (q-b)*(q-b))

# define pp(n) printf("%.10Lf",n);
# define line cout<<"\n";

string vow = "aeiou";
const ld pie = 3.14159265358979 ;

const int dxhorse[] = {-2, -2, -1, -1, 1, 1, 2, 2};
const int dyhorse[] = {1, -1, 2, -2, 2, -2, 1, -1};
const ll mod = 1e9 + 7 ;

vi parent ;

int find_parent ( int indx )
{
    if ( parent[indx] < 0 )
        return indx ;

    parent[indx] = find_parent ( parent[indx] ) ;
    return parent[indx] ;
}

void join ( int a , int b ) // a and b are index
{
    int p1 = find_parent ( a ) ;
    int p2 = find_parent ( b ) ;

    if ( p1 == p2 ) return ;
    if ( parent[p1] > parent[p2] ) swap ( p1 , p2 ) ;

    parent[p1] += parent[p2] ;
    parent[p2] = p1 ;
}

void solve ( int test_case )
{
    int n ;
    cin >> n ;

    vpii v ( n ) ;
    for ( int i=0 ; i < n ; i++ ) cin >> v[i].F >> v[i].S ;

    parent = vi ( n , -1 ) ;
    for ( int i=0 ; i < n ; i++ )
    {
        for ( int j = 0 ; j < n ; j++ )
        {
            if ( v[i].F == v[j].F || v[i].S == v[j].S ) // then these are already connected
                join ( i , j ) ;
        }
    }

    int ans = 0 ;
    for ( int j : parent )
        ans += ( j < 0 ) ;

    cout << ans - 1 ; line
}

int main()
{
    fast
    int t = 1;
    //cin >> t;

    for ( int i=0 ; i < t ; i++ ) solve(i);
    return 0;
}


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

const int N = 102 ;
int parent[N] ;

int find_parent ( int node )
{
    if ( parent[node] < 0 )
        return node ;

    return parent[node] = find_parent ( parent[node] ) ;
}

void join ( int a , int b )
{
    int p1 = find_parent ( a ) ;
    int p2 = find_parent ( b ) ;

    if ( p1 == p2 )
        return ;

    if ( parent[p1] > parent[p2] )
        swap ( p1 , p2 ) ;

    parent[p1] += parent[p2] ;
    parent[p2] = p1 ;
}

int main()
{
    int n ;
    cin >> n ;

    vector < pair < int , int > > v ( n ) ;

    for ( int i=0 ; i < n ; i ++ )
    {
        cin >> v[i].first ;
        cin >> v[i].second ;
    }

    for ( int i=0 ; i < n ; i ++ )
        parent[i] = -1 ;

    for ( int i=0 ; i < n ; i ++ )
    {
        for ( int j = i + 1 ; j < n ; j ++ )
        {
            if ( v[i].first == v[j].first || v[i].second == v[j].second )
                join ( i , j ) ;
        }
    }

    int connected_components = 0 ;

    for ( int i=0 ; i < n ; i ++ )
    {
        if ( parent[i] < 0 )
            connected_components ++ ;
    }

    int ans = connected_components - 1 ;
    cout << ans ;

    return 0 ;
}


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

# define C continue;
# define R return

# define D double
# define I insert
# define ll long long
# define ld long double
# define ull unsigned long long

# define pb push_back
# define vi vector < int >
# define vc vector < char >
# define vs vector < string >
# define vb vector < bool >
# define vd vector < D >
# define vll vector < ll >
# define vull vector < ull >

# define vvi vector < vector < int > >
# define vvb vector < vector < bool > >
# define vvc vector < vector < char > >
# define vvll vector < vector < ll > >
# define vvd vector < vector < D > >
# define vld vector < ld >

# define all(v) (v).begin() , (v).end()
# define allrev(v) (v).rbegin() , (v).rend()

# define pii pair < int , int >
# define vpii vector < pii >
# define vvpii vector < vector < pii > >
# define F first
# define S second
# define mp make_pair

# define fast ios_base::sync_with_stdio(false) ; cin.tie(0) ; cout.tie(0);
# define dist(a,b,p,q) sqrt((p-a)*(p-a) + (q-b)*(q-b));
# define mod 1000000007

# define pp(n) printf("%.10f",n);
# define line cout<<"\n";

const D pie = acos(-1);
const D hpie = asin(1);
const D qpie = atan2(1,1);
const D tpie = 2*pie;

string vow = "aeiou";
int n = 1001;
vs v ( n );
vb vis ( n , false );

void solve( int test_case )
{
    string temp = "";
    for ( int i=0 ; i<n ; i++ )
        temp += '.';

    for ( int i=0 ; i<n ; i++ ) v[i] = temp;

    int t;
    cin >> t;

    int x , y;
    while ( t-- )
    {
        cin >> x >> y;
        v[x][y] = 'x';
    }

    int cnt = -1;

    for ( int j = 1 ; j<n ; j++ )
    {
        int temp_cnt = 0;
        bool connected = false;
        for ( int i=1 ; i<n ; i++ )
        {
            if ( v[i][j]=='x' )
            {
                temp_cnt++;
                if ( vis[i] ) connected = true;
            }
        }

        if ( temp_cnt )
        {
            if ( !connected ) cnt++;
            for ( int i=1 ; i<n ; i++ )
            {
                if ( v[i][j]=='x' )
                {
                    vis[i] = true;
                }
            }
        }
    }

    cout << cnt ;
    line

}

int main()
{
    int t = 1;
    //cin>>t;

    for ( int i=0 ; i<t ; i++ ) solve(i);
    return 0;
}


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

# define C continue;
# define R return

# define D double
# define I insert
# define ll long long
# define ld long double

# define ull unsigned long long
# define ui unsigned int

# define pb push_back
# define pf push_front

# define vi vector < int >
# define vc vector < char >
# define vs vector < string >
# define vb vector < bool >
# define vd vector < D >
# define vll vector < ll >
# define vull vector < ull >
# define vld vector < ld >
# define PQ priority_queue

# define vvi vector < vector < int > >
# define vvb vector < vector < bool > >
# define vvc vector < vector < char > >
# define vvs vector < vs >
# define vvll vector < vector < ll > >
# define vvd vector < vector < D > >
# define vvld vector < vector < ld > >

# define all(v) (v).begin() , (v).end()
# define allrev(v) (v).rbegin() , (v).rend()
# define allcomp(v) v.begin() , v.end() , comp
# define allrevcomp(v) v.rbegin() , v.rend() , comp

# define pii pair < int , int >
# define pll pair < ll , ll >
# define pld pair < ld , ld >
# define pDD pair < D , D >

# define vpld vector < pld >
# define vpii vector < pii >
# define vpll vector < pll >
# define vpDD vector < pDD >

# define vvpii vector < vector < pii > >
# define F first
# define S second
# define mp make_pair

# define dist(a,b,p,q) sqrt((p-a)*(p-a) + (q-b)*(q-b))

# define pp(n) printf("%.10Lf",n);
# define line cout<<"\n";
# define fast ios_base::sync_with_stdio(false) ; cin.tie(0) ; cout.tie(0);

string vow = "aeiou";
int month[] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

const int dxhorse[] = {-2, -2, -1, -1, 1, 1, 2, 2};
const int dyhorse[] = {1, -1, 2, -2, 2, -2, 1, -1};

const int dx[] = { -1 , 0 , 1 , 0 } ;
const int dy[] = { 0 , 1 , 0 , -1 } ;

const ld pie = 3.1415926535897932384626 ;
const ll mod = 1e9 + 7 ;

/// Tip : If a and b are positive integers ; we may say - ceil (a/b) = 1 + floor ( (a-1)/b ) .

# define piii pair < pii , int >
vector < piii > v ;

int n ;

vector < int > parent ;

int find_parent ( int node )
{
    if ( parent[node] < 0 )
        return node ;

    return parent[node] = find_parent ( parent[node] ) ;
}

void join ( int a , int b )
{
    int p1 = find_parent ( a ) ;
    int p2 = find_parent ( b ) ;

    if ( p1 == p2 ) return ;

    if ( parent[p1] > parent[p2] )
        swap ( p1 , p2 ) ;

    parent[p1] += parent[p2] ;
    parent[p2] = p1 ;
}

void connect()
{
    for ( int i=0 ; i < n - 1 ; i ++ )
    {
        if ( v[i].F.F == v[i + 1].F.F )
            join ( v[i].S , v[i + 1].S ) ;
    }
}

void solve ( int test_case )
{
    cin >> n ;

    parent.resize ( n ) ;
    v.resize ( n ) ;

    for ( int i=0 ; i < n ; i ++ )
        parent[i] = -1 ;

    for ( int i=0 ; i < n ; i ++ )
    {
        cin >> v[i].F.F >> v[i].F.S ;
        v[i].S = i ;
    }

    sort ( all ( v ) ) ;
    connect( ) ;

    for ( int i=0 ; i < n ; i ++ )
    {
        swap ( v[i].F.F , v[i].F.S ) ;
    }

    sort ( all ( v ) ) ;
    connect ( ) ;

    int components = -1 ;

    for ( int i=0 ; i < n ; i ++ )
    {
        if ( parent[i] < 0 )
            components ++ ;
    }

    cout << components ; line ;
}

int main()
{fast
    int t = 1;
    // cin >> t;

    for ( int i=1 ; i <= t ; i ++ ) solve ( i ) ;
    return 0;
}


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

# define C continue;
# define R return

# define D double
# define I insert
# define ll long long
# define ld long double

# define ull unsigned long long
# define ui unsigned int

# define pb push_back
# define pf push_front

# define vi vector < int >
# define vc vector < char >
# define vs vector < string >
# define vb vector < bool >
# define vd vector < D >
# define vll vector < ll >
# define vull vector < ull >
# define vld vector < ld >

# define vvi vector < vector < int > >
# define vvb vector < vector < bool > >
# define vvc vector < vector < char > >
# define vvll vector < vector < ll > >
# define vvd vector < vector < D > >
# define vvld vector < vector < ld > >

# define all(v) (v).begin() , (v).end()
# define allrev(v) (v).rbegin() , (v).rend()
# define allcomp(v) v.begin() , v.end() , comp
# define allrevcomp(v) v.rbegin() , v.rend() , comp

# define pii pair < int , int >
# define pll pair < ll , ll >
# define pld pair < ld , ld >
# define pDD pair < D , D >

# define vpld vector < pld >
# define vpii vector < pii >
# define vpll vector < pll >
# define vpDD vector < pDD >

# define vvpii vector < vector < pii > >
# define F first
# define S second
# define mp make_pair

# define fast ios_base::sync_with_stdio(false) ; cin.tie(0) ; cout.tie(0);
# define dist(a,b,p,q) sqrt((p-a)*(p-a) + (q-b)*(q-b))

# define pp(n) printf("%.10Lf",n);
# define line cout<<"\n";

string vow = "aeiou";
const ld pie = 3.14159265358979 ;

const int dxhorse[] = {-2, -2, -1, -1, 1, 1, 2, 2};
const int dyhorse[] = {1, -1, 2, -2, 2, -2, 1, -1};
const ll mod = 1e9 + 7 ;

vi parent ;

int find_parent ( int indx )
{
    if ( parent[indx] < 0 )
        return indx ;

    parent[indx] = find_parent ( parent[indx] ) ;
    return parent[indx] ;
}

void join ( int a , int b ) // a and b are index
{
    int p1 = find_parent ( a ) ;
    int p2 = find_parent ( b ) ;

    if ( p1 != p2 ) parent[p2] = p1 ;
}

void solve ( int test_case )
{
    int n ;
    cin >> n ;

    vpii v ( n ) ;
    for ( int i=0 ; i < n ; i++ ) cin >> v[i].F >> v[i].S ;

    parent = vi ( n , -1 ) ;
    for ( int i=0 ; i < n ; i++ )
    {
        for ( int j = 0 ; j < n ; j++ )
        {
            if ( v[i].F == v[j].F || v[i].S == v[j].S ) // then these are already connected
                join ( i , j ) ;
        }
    }

    int ans = 0 ;
    for ( int j : parent )
        ans += ( j < 0 ) ;

    cout << ans - 1 ; line
}

int main()
{
    int t = 1;
    //cin >> t;

    for ( int i=0 ; i < t ; i++ ) solve(i);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
map<pii,bool> mp;
int dp[1001][1001];
signed main()
{
   int n;
   cin>>n;
   for(int i=0;i<1001;i++)
   {
       for(int j=0;j<1001;j++)
        dp[i][j]=0;
   }

   for(int i=0;i<n;i++)
   {
       int x,y;
       cin>>x>>y;
       dp[x][y]=1;
       mp[{x,y}]=false;
   }
   int an=n-1;
   for(int i=0;i<1001;i++)
   {
       for(int j=0;j<1001;j++)
       {
           if(dp[i][j]==1)
           {
               dp[i][j]=0;
               for(int k=0;k<1001;k++)
               {
                   if(dp[k][j]==1&&!mp[{k,j}])
                   {
                       an--;
                       mp[{k,j}]=true;
                   }
                   if(dp[i][k]==1&&!mp[{i,k}])
                   {
                       an--;
                       mp[{i,k}]=true;
                   }
               }
           }
       }
   }
   cout<<an;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
# define int long long
#define pii pair<int,int>
map<pii,bool> mp;
int dp[1001][1001];
int p[105] = {0};
int a[105][2] = {0};

signed par(int v)
{
    if(p[v] != v)
        return p[v] = par(p[v]);
    return v;
}

void join(int v1, int v2)
{
    v1 = par(v1);
    v2 = par(v2);
    if(v1 != v2)
        p[v1] = v2;
}

signed main()
{
    int n;
   cin>>n;

    for(int i = 0; i < n; i++)
    {
       cin>>a[i][0]>>a[i][1];
        p[i] = i;
    }

    for(int i = 0; i < n; i++)
    {
        for(int  j = i+1; j < n; j++)
        {
            if(a[i][0] == a[j][0] || a[i][1] == a[j][1])
                join(i, j);
        }
    }

    int cnt = 0;

    for(int i = 0; i < n; i++)
        p[i] = par(i);

    sort(p, p + n);

    for(int i = 1; i < n; i++)
    {
        if(p[i] != p[i-1])
            cnt++;
    }

    cout<<cnt;

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
vector<int>adj[1001];
int vis[1001];
int x[101],y[101];
void dfs(int s)
{
    vis[s]=1;
    for(int child:adj[s])
    {
        if(vis[child]==0)
          dfs(child);
    }
}

int main()
{
    int n;
    cin>>n;

    for(int i=0;i<=1000;i++)
       vis[i]=0;
    for(int i=1;i<=n;i++)
    {   int a,b;
    cin>>a>>b;
        adj[a].push_back(b);
        adj[b].push_back(a);
        x[i]=a;
        y[i]=b;
    }
    int ct=-1;
    for(int i=1;i<=n;i++)
    {
        if(vis[x[i]]!=1)
        { dfs(i);

         ct++;}
    }
    cout<<ct<<endl;

}


#include<bits/stdc++.h>
using namespace std;
int n,x[101],y[101],ans=-1,vis[101];
void dfs(int a){
	vis[a]=1;
	for(int i=1;i<=n;i++)if((x[i]==x[a] || y[i]==y[a]) && !vis[i])dfs(i);
}

int main(){
	cin>>n;
	for(int i=1;i<=n;i++)cin>>x[i]>>y[i];
	for(int i=1;i<=n;i++)if(!vis[i])dfs(i),ans++;
	cout<<ans<<endl;
}


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

int ar[102][102];
bool vis[102][102];
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
int n;
bool isvaild(int x, int y)
{
    if(x<1 || x>n || y<1 || y>n)
      return false;

     if(vis[x][y]==true || ar[x][y]==0)
         return false;

    return false;
}

void dfs(int x, int y)
{
    vis[x][y]=1;
    for(int i=0;i<4;i++)
    {
        if(isvalid(x+dx[i],y+dy[i]))
        dfs(x+dx[i],y+dy[i]);
    }
}

int main()
{
    cin>>n;
    for(int i=0;i<=101;i++)
     {
         for(int j=0;j<=101;j++)
          {
              ar[i][j]=0;
              vis[i][j]=false;
          }
     }

      for(int i=0;i<n;i++)
      {
          int x,y;
          cin>>x>>y;
          ar[x][y]=1;
      }

    int ct=-1;
    for(int i=0;i<=101;i++)
     {
         for(int j=0;j<=101;j++)
          {
              if(ar[i][j]==1 && vis[i][j]==false)
                 {
                     ct++;
                     dfs(i,j);
                 }
          }
     }
     cout<<ct<<endl;

}


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

int ar[102][102];
bool vis[102][102];
int dx={-1,0,1,0};
int dy={0,1,0,-1};
int n;
bool isvaild(int x, int y)
{
    if(x<1 || x>n || y<1 || y>n)
      return false;

     if(vis[x][y]==true || ar[x][y]==0)
         return false;

    return false;
}

void dfs(int x, int y)
{
    vis[x][y]=1;
    for(int i=0;i<4;i++)
    {
        if(isvalid(x+dx[i],y+dy[i]))
        dfs(x+dx[i],y+dy[i]);
    }
}

int main()
{
    cin>>n;
    for(int i=0;i<=101;i++)
     {
         for(int j=0;j<=101;j++)
          {
              ar[i][j]=0;
              vis[i][j]=false;
          }
     }

      for(int i=0;i<n;i++)
      {
          int x,y;
          cin>>x>>y;
          ar[x][y]=1;
      }

    int ct=-1;
    for(int i=0;i<=101;i++)
     {
         for(int j=0;j<=101;j++)
          {
              if(ar[i][j]===1 && vis[i][j]==false)
                 {
                     ct++;
                     dfs(i,j);
                 }
          }
     }
     cout<<ct<<endl;

}


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

int ar[102][102];
bool vis[102][102];
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
int n;
bool isvalid(int x, int y)
{
    if(x<1 || x>n || y<1 || y>n)
      return false;

     if(vis[x][y]==true || ar[x][y]==0)
         return false;

    return true;
}

void dfs(int x, int y)
{
    vis[x][y]=1;
    for(int i=0;i<4;i++)
    {
        if(isvalid(x+dx[i],y+dy[i]))
        dfs(x+dx[i],y+dy[i]);
    }
}

int main()
{
    cin>>n;
    for(int i=0;i<=101;i++)
     {
         for(int j=0;j<=101;j++)
          {
              ar[i][j]=0;
              vis[i][j]=false;
          }
     }

      for(int i=0;i<n;i++)
      {
          int x,y;
          cin>>x>>y;
          ar[x][y]=1;
      }

    int ct=-1;
    for(int i=0;i<=101;i++)
     {
         for(int j=0;j<=101;j++)
          {
              if(ar[i][j]==1 && vis[i][j]==false)
                 {
                     ct++;
                     dfs(i,j);
                 }
          }
     }
     cout<<ct<<endl;
}


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

int ar[102][102];
bool vis[102][102];
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
int n;
bool isvalid(int x, int y)
{
    if(x<1 || x>n || y<1 || y>n)
      return false;

     if(vis[x][y]==true || ar[x][y]==0)
         return false;

    return false;
}

void dfs(int x, int y)
{
    vis[x][y]=1;
    for(int i=0;i<4;i++)
    {
        if(isvalid(x+dx[i],y+dy[i]))
        dfs(x+dx[i],y+dy[i]);
    }
}

int main()
{
    cin>>n;
    for(int i=0;i<=101;i++)
     {
         for(int j=0;j<=101;j++)
          {
              ar[i][j]=0;
              vis[i][j]=false;
          }
     }

      for(int i=0;i<n;i++)
      {
          int x,y;
          cin>>x>>y;
          ar[x][y]=1;
      }

    int ct=-1;
    for(int i=0;i<=101;i++)
     {
         for(int j=0;j<=101;j++)
          {
              if(ar[i][j]==1 && vis[i][j]==false)
                 {
                     ct++;
                     dfs(i,j);
                 }
          }
     }
     cout<<ct<<endl;

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
vector<int>adj[1001];
int vis[1001];

void dfs(int s)
{
    vis[s]=1;
    for(int child:adj[s])
    {
        if(vis[child]==0)
          dfs(child);
    }
}

int main()
{
    int n;
    cin>>n;
    for(int i=0;i<=1000;i++)
       vis[i]=0;
    while(n--)
    {   int a,b;
    cin>>a>>b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    int ct=-1;
    for(int i=1;i<=1000;i++)
    {
        if(vis[i]!=1)
        { dfs(i);
         ct++;}
    }
    cout<<ct<<endl;

}


#include <bits/stdc++.h>
using namespace std;
//map<pair<int,int>,vector<pair<int,int > > >v;
int main()
{
    int n;
    cin>>n;
    pair<int,int> p[n];
    for(int i=0;i<n;i++)
        cin>>p[i].first>>p[i].second;

    map<pair<int,int> ,int> mm;

    int cnt=0;
    for (int i = 0; i < n; ++i) {
        if(mm[p[i]]==0)
        {
            cnt++;
            mm[p[i]]=1;
            for (int j = 0; j < n; ++j) {
                if(mm[p[j]]==0)
                {
                    if(p[j].first==p[i].first||p[j].second==p[i].second)
                    {
//                        v[p[i]].push_back(p[j]);
                        mm[p[j]]=1;
                    }
                }
            }
        }
    }

    cout<<cnt-1;
}


#include <iostream>
using namespace std;
int main()
{
    int cnt=0;
    int n;
    cin>>n;
    int x[n],y[n];
    for (int i = 0; i < n; ++i) {
        cin>>x[i]>>y[i];
    }

    for (int i = 1; i < n; ++i) {
        if(x[i-1]==x[i]||y[i-1]==y[i])
            continue;
        else
            cnt++;
    }
    cout<<cnt;
}


#include <bits/stdc++.h>
using namespace std;
//map<pair<int,int>,vector<pair<int,int > > >v;
int main()
{
    int n;
    cin>>n;
    pair<int,int> p[n];
    for(int i=0;i<n;i++)
        cin>>p[i].first>>p[i].second;

    map<int,int> mm;
    map<int,int> ma;
    int cnt=0;
    for (int i = 0; i < n; ++i) {
        if(mm[p[i].first]==0 && ma[p[i].second]==0)
        {
            cnt++;
            mm[p[i].first]=1;
            ma[p[i].second]=1;
        }
    }

    cout<<cnt-1;
}


#include <bits/stdc++.h>
using namespace std;
//map<pair<int,int>,vector<pair<int,int > > >v;

bool compare(pair<int,int> a,pair<int,int> b)
{
    if (a.first==b.first||b.second==a.second)
        return true;
    return false;
}

int main()
{
    int n;
    cin>>n;
    pair<int,int> p[n];
    for(int i=0;i<n;i++)
        cin>>p[i].first>>p[i].second;

    sort(p,p+n,compare);
    map<int,int> mm;
    map<int,int> ma;
    int cnt=0;
    for (int i = 0; i < n; ++i) {
        if(mm[p[i].first]==0 && ma[p[i].second]==0)
        {
            cnt++;
            mm[p[i].first]=1;
            ma[p[i].second]=1;
            for (int j = 0; j < n; ++j) {
                if(mm[p[j].first]==1 || ma[p[j].second]==1) {
                    mm[p[j].first] = 1;
                    ma[p[j].second] = 1;
                }
            }
            for (int j = 0; j < n; ++j) {
                if(mm[p[j].first]==1 || ma[p[j].second]==1) {
                    mm[p[j].first] = 1;
                    ma[p[j].second] = 1;
                }
            }

        }
        else
        {
            mm[p[i].first]=1;
            ma[p[i].second]=1;
            for (int j = 0; j < n; ++j) {
                if(mm[p[j].first]==1 || ma[p[j].second]==1) {
                    mm[p[j].first] = 1;
                    ma[p[j].second] = 1;
                }
            }
            for (int j = 0; j < n; ++j) {
                if(mm[p[j].first]==1 || ma[p[j].second]==1) {
                    mm[p[j].first] = 1;
                    ma[p[j].second] = 1;
                }
            }

        }
    }

    cout<<cnt-1;
}


#include <bits/stdc++.h>
using namespace std;
//map<pair<int,int>,vector<pair<int,int > > >v;
int main()
{
    int n;
    cin>>n;
    pair<int,int> p[n];
    for(int i=0;i<n;i++)
        cin>>p[i].first>>p[i].second;

    map<int,int> mm;
    map<int,int> ma;
    int cnt=0;
    for (int i = 0; i < n; ++i) {
        if(mm[p[i].first]==0 && ma[p[i].second]==0)
        {
            cnt++;
        }
            mm[p[i].first]=1;
            ma[p[i].second]=1;
    }

    cout<<cnt-1;
}


#include <bits/stdc++.h>
using namespace std;
//map<pair<int,int>,vector<pair<int,int > > >v;

bool compare(pair<int,int> a,pair<int,int> b)
{
    if (a.first==b.first||b.second==a.second)
        return true;
    return false;
}

int main()
{
    int n;
    cin>>n;
    pair<int,int> p[n];
    for(int i=0;i<n;i++)
        cin>>p[i].first>>p[i].second;

    sort(p,p+n,compare);
    map<int,int> mm;
    map<int,int> ma;
    int cnt=0;
    for (int i = 0; i < n; ++i) {
        if(mm[p[i].first]==0 && ma[p[i].second]==0)
        {
            cnt++;
            mm[p[i].first]=1;
            ma[p[i].second]=1;
            for (int j = 0; j < n; ++j) {
                if(mm[p[j].first]==1 || ma[p[j].second]==1) {
                    mm[p[j].first] = 1;
                    ma[p[j].second] = 1;
                }
            }
        }
        else
        {
            mm[p[i].first]=1;
            ma[p[i].second]=1;
            for (int j = 0; j < n; ++j) {
                if(mm[p[j].first]==1 || ma[p[j].second]==1) {
                    mm[p[j].first] = 1;
                    ma[p[j].second] = 1;
                }
            }

        }
    }

    cout<<cnt-1;
}


#include <bits/stdc++.h>
using namespace std;
//map<pair<int,int>,vector<pair<int,int > > >v;
int main()
{
    int n;
    cin>>n;
    pair<int,int> p[n];
    for(int i=0;i<n;i++)
        cin>>p[i].first>>p[i].second;

    map<int,int> mm;
    map<int,int> ma;
    int cnt=0;
    for (int i = 0; i < n; ++i) {
        if(mm[p[i].first]==0 && ma[p[i].second]==0)
        {
            cnt++;
            mm[p[i].first]=1;
            ma[p[i].second]=1;
            for (int j = 0; j < n; ++j) {
                if(mm[p[j].first]==1 || ma[p[j].second]==1) {
                    mm[p[j].first] = 1;
                    ma[p[j].second] = 1;
                }
            }
        }
            mm[p[i].first]=1;
            ma[p[i].second]=1;
    }

    cout<<cnt-1;
}


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

// using namespace std;

// void dfs(int n, int g[][2], int used[], int ind) {
//     used[ind] = 1;
//     for(int i=0; i<n; i++) {
//         if(used[i]==0 && (g[i][0]==g[ind][0] || g[i][1]==g[ind][1]))    dfs(n, g, used, i);
//     }
// }

// int main() {
//     int n, ans = 0;
//     cin>>n;
//     int g[n][2];
//     int used[n];
//     for(int i=0; i<n; i++) {
//         cin>>g[i][0]>>g[i][1];
//         used[i] = 0;
//     }
//     for(int i=0; i<n; i++) {
//         if(used[i]==0) {
//             dfs(n, g, used, i);
//             ans++;
//         }
//     }
//     cout<<ans-1<<endl;

//     return 0;
// }

#include <bits/stdc++.h>

using namespace std;

int main() {
    int n, x, y, t1, t2, ans = 0;
    cin>>n;
    int g[n][2];
    map<pair<int, int>, int> mp;
    for(int i=0; i<n; i++) {
        cin>>g[i][0]>>g[i][1];
        if(i==0) {
            mp[make_pair(g[i][0], 0)] = 0;
            mp[make_pair(g[i][1], 1)] = 1;
        }
        else {
            if(mp.find(make_pair(g[i][0], 0))!=mp.end() || mp.find(make_pair(g[i][1], 1))!=mp.end()) {
                mp[make_pair(g[i][0], 0)] = 0;
                mp[make_pair(g[i][1], 1)] = 1;
            }
        }
    }
    // cout<<mp.size()<<endl;
    x = g[0][0];
    y = g[1][0];
    for(int i=1; i<n; i++) {
        t1 = g[i][0];
        t2 = g[i][1];
        if(mp.find(make_pair(t1, 0))==mp.end() && mp.find(make_pair(t2, 1))==mp.end()) {
            ans++;
        }
        mp[make_pair(t1, 0)] = 0;
        mp[make_pair(t2, 1)] = 1;
    }

    cout<<ans;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int n, x, y, t1, t2, ans = 0;
    cin>>n;
    int g[n][2];
    map<pair<int, int>, int> mp;
    for(int i=0; i<n; i++) {
        cin>>g[i][0]>>g[i][1];
        if(i==0) {
            mp[make_pair(g[i][0], 0)] = 0;
            mp[make_pair(g[i][1], 1)] = 1;
        }
        else {
            if(mp.find(make_pair(g[i][0], 0))!=mp.end() || mp.find(make_pair(g[i][1], 1))!=mp.end()) {
                mp[make_pair(g[i][0], 0)] = 0;
                mp[make_pair(g[i][1], 1)] = 1;
            }
        }
    }
    // cout<<mp.size()<<endl;
    x = g[0][0];
    y = g[1][0];
    for(int i=1; i<n; i++) {
        t1 = g[i][0];
        t2 = g[i][1];
        if(mp.find(make_pair(t1, 0))==mp.end() && mp.find(make_pair(t2, 1))==mp.end()) {
            ans++;
            mp[make_pair(t1, 0)] = 0;
            mp[make_pair(t2, 1)] = 1;
        }
    }

    cout<<ans;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int n, x, y, t1, t2, ans = 0;
    cin>>n;
    int g[n][2];
    map<pair<int, int>, int> mp;
    for(int i=0; i<n; i++) {
        cin>>g[i][0]>>g[i][1];
        if(i==0) {
            mp[make_pair(g[i][0], 0)] = 0;
            mp[make_pair(g[i][1], 1)] = 1;
        }
        else {
            if(mp.find(make_pair(g[i][0], 0))!=mp.end() || mp.find(make_pair(g[i][1], 1))!=mp.end()) {
                mp[make_pair(g[i][0], 0)] = 0;
                mp[make_pair(g[i][1], 0)] = 1;
            }
        }
    }
    // cout<<mp.size()<<endl;
    x = g[0][0];
    y = g[1][0];
    for(int i=1; i<n; i++) {
        t1 = g[i][0];
        t2 = g[i][1];
        if(mp.find(make_pair(t1, 0))==mp.end() && mp.find(make_pair(t2, 1))==mp.end()) {
            ans++;
            mp[make_pair(t1, 0)] = 0;
            mp[make_pair(y, 1)] = 1;
        }
    }

    cout<<ans;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int n, x, y, t1, t2, ans = 0;
    cin>>n;
    map<int, int> mp;
    for(int i=0; i<n; i++) {
        if(i==0) {
            cin>>x>>y;
            mp[x] = 0;
            mp[y] = 1;
        }
        else {
            cin>>t1>>t2;
            if((mp.find(t1)==mp.end() || mp[t1]==1) && (mp.find(t2)==mp.end() || mp[t2]==0)) {
                ans++;
                mp[t1] = 0;
                mp[y] = 1;
            }
        }
    }

    cout<<ans;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int n, x, y, t1, t2, ans = 0;
    cin>>n;
    int g[n][2];
    map<pair<int, int>, int> mp;
    for(int i=0; i<n; i++) {
        cin>>g[i][0]>>g[i][1];
        if(i==0) {
            mp[make_pair(g[i][0], 0)] = 0;
            mp[make_pair(g[i][1], 1)] = 1;
        }
        else {
            if(mp.find(make_pair(g[i][0], 0))!=mp.end() || mp.find(make_pair(g[i][1], 1))!=mp.end()) {
                mp[make_pair(g[i][0], 0)] = 0;
                mp[make_pair(g[i][1], 0)] = 1;
            }
        }
    }
    // cout<<mp.size()<<endl;
    x = g[0][0];
    y = g[1][0];
    for(int i=1; i<n; i++) {
        t1 = g[i][0];
        t2 = g[i][1];
        if(mp.find(make_pair(t1, 0))==mp.end() && mp.find(make_pair(t2, 1))==mp.end()) {
            ans++;
            mp[make_pair(x, 0)] = 0;
            mp[make_pair(t2, 1)] = 1;
            mp[make_pair(t1, 0)] = 0;
        }
    }

    cout<<ans;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
    int n, x, y, t1, t2, ans = 0;
    cin>>n;
    int g[n][2];
    map<pair<int, int>, int> mp;
    for(int i=0; i<n; i++) {
        cin>>g[i][0]>>g[i][1];
        if(i==0) {
            mp[make_pair(g[i][0], 0)] = 0;
            mp[make_pair(g[i][1], 1)] = 1;
        }
        else {
            if(mp.find(make_pair(g[i][0], 0))!=mp.end() || mp.find(make_pair(g[i][1], 1))!=mp.end()) {
                mp[make_pair(g[i][0], 0)] = 0;
                mp[make_pair(g[i][1], 0)] = 1;
            }
        }
    }
    // cout<<mp.size()<<endl;
    x = g[0][0];
    y = g[1][0];
    for(int i=1; i<n; i++) {
        t1 = g[i][0];
        t2 = g[i][1];
        if(mp.find(make_pair(t1, 0))==mp.end() && mp.find(make_pair(t2, 1))==mp.end()) {
            ans++;
            mp[make_pair(t1, 0)] = 0;
            mp[make_pair(t2, 1)] = 1;
        }
    }

    cout<<ans;

    return 0;
}


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

// using namespace std;

// void dfs(int n, int g[][2], int used[], int ind) {
//     used[ind] = 1;
//     for(int i=0; i<n; i++) {
//         if(used[i]==0 && (g[i][0]==g[ind][0] || g[i][1]==g[ind][1]))    dfs(n, g, used, i);
//     }
// }

// int main() {
//     int n, ans = 0;
//     cin>>n;
//     int g[n][2];
//     int used[n];
//     for(int i=0; i<n; i++) {
//         cin>>g[i][0]>>g[i][1];
//         used[i] = 0;
//     }
//     for(int i=0; i<n; i++) {
//         if(used[i]==0) {
//             dfs(n, g, used, i);
//             ans++;
//         }
//     }
//     cout<<ans-1<<endl;

//     return 0;
// }

#include <bits/stdc++.h>

using namespace std;

int main() {
    int n, x, y, t1, t2, ans = 0;
    cin>>n;
    int g[n][2];
    map<pair<int, int>, int> mp;
    for(int i=0; i<n; i++) {
        cin>>g[i][0]>>g[i][1];
        if(i==0) {
            mp[make_pair(g[i][0], 0)] = 0;
            mp[make_pair(g[i][1], 1)] = 1;
        }
        else {
            if(mp.find(make_pair(g[i][0], 0))!=mp.end() || mp.find(make_pair(g[i][1], 1))!=mp.end()) {
                mp[make_pair(g[i][0], 0)] = 0;
                mp[make_pair(g[i][1], 1)] = 1;
            }
        }
    }
    // cout<<mp.size()<<endl;
    x = g[0][0];
    y = g[1][0];
    for(int i=1; i<n; i++) {
        t1 = g[i][0];
        t2 = g[i][1];
        if(mp.find(make_pair(t1, 0))==mp.end() && mp.find(make_pair(t2, 1))==mp.end()) {
            ans++;
            mp[make_pair(t1, 0)] = 0;
            mp[make_pair(t2, 1)] = 1;
        }
        else {
            mp[make_pair(t1, 0)] = 0;
            mp[make_pair(t2, 1)] = 1;
        }
    }

    cout<<ans;

    return 0;
}


#include <bits/stdc++.h>

//PROBLEM: http://codeforces.com/problemset/problem/217/A
/**
Uses DSU to find connected component

**/

using namespace std;

int parent[1000001];
int rank[1000001];
int find(int x)
{
	if(parent[x]==-1)
	{
		return x;
	}
	return find(parent[x]);
}

void unionx(int x, int y)
{
	int px = find(x);
	int py = find(y);
	if(rank[px]>rank[py])
	{
		parent[py] = px;
	}
	else if(rank[px]<rank[py])
	{
		parent[px] = py;
	}
	else
	{
		parent[px] = py;
		rank[px]++;
	}
}

int main()
{
	ios_base::sync_with_stdio(false);
	int n;
	cin>>n;
	int x[n];
	int y[n];
	for(int i=0;i<n;i++)
	{
		int u,v;
		cin>>u>>v;
		x[i] = u;
		y[i] = v;
		parent[i]=-1;
		rank[i]=0;
	}
	for(int i=0;i<n;i++)
	{
		for(int j=i+1;j<n;j++)
		{
			if(x[i]==x[j] || y[i]==y[j])
			{
				unionx(i,j);
			}
		}
	}
	int ans =0;
	for(int i=0;i<n;i++)
	{
		if(parent[i]==-1)
		{
			ans++;
		}
	}
	cout<<ans-1;
	return 0;
}


#include <bits/stdc++.h>

//PROBLEM: http://codeforces.com/problemset/problem/217/A
/**
Uses DSU to find connected component
Note don't use names linke union and rank as they are ambiguous

**/

using namespace std;

int parent[1000001];
int rankx[1000001];
int find(int x)
{
	if(parent[x]==-1)
	{
		return x;
	}
	parent[x] = find(parent[x]);
	return parent[x];
}

void unionx(int x, int y)
{
	int px = find(x);
	int py = find(y);
	if(rankx[px]>rankx[py])
	{
		parent[py] = px;
	}
	else if(rankx[px]<rankx[py])
	{
		parent[px] = py;
	}
	else
	{
		parent[px] = py;
		rankx[px]++;
	}
}

int main()
{
	ios_base::sync_with_stdio(false);
	int n;
	cin>>n;
	int x[n];
	int y[n];
	for(int i=0;i<n;i++)
	{
		int u,v;
		cin>>u>>v;
		x[i] = u;
		y[i] = v;
		parent[i]=-1;
		rankx[i]=0;
	}
	for(int i=0;i<n;i++)
	{
		for(int j=i+1;j<n;j++)
		{
			if(x[i]==x[j] || y[i]==y[j])
			{
				unionx(i,j);
			}
		}
	}
	int ans =0;
	for(int i=0;i<n;i++)
	{
		if(parent[i]==-1)
		{
			ans++;
		}
	}
	cout<<ans-1;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int parent[1000001];

int find(int x)
{
	if(parent[x]==-1)
	{
		return x;
	}
	return find(parent[x]);
}

void union(int x, int y)
{
	int px = find(x);
	int py = find(y);
	if(px!=py)
	{
		parent[x] = y;
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	int n;
	cin>>n;
	int x[n],y[n];
	for(int i=0;i<n;i++)
	{
		int u,v;
		cin>>u>>v;
		x[i] = u;
		y[i] = v;
		parent[i]=-1;
	}
	for(int i=0;i<n;i++)
	{
		for(int j=i+1;j<n;j++)
		{
			if(x[i]==x[j] || y[i]==y[j])
			{
				union(i,j);
			}
		}
	}
	int ans =0;
	for(int i=0;i<n;i++)
	{
		if(parent[i]==-1)
		{
			ans++;
		}
	}
	cout<<ans-1;

}


#include <bits/stdc++.h>

//PROBLEM: http://codeforces.com/problemset/problem/217/A
/**
Uses DSU to find connected component
Note don't use names linke union and rank as they are ambiguous

**/

using namespace std;

int parent[1000001];
int rankx[1000001];
int find(int x)
{
	if(parent[x]==i)
	{
		return x;
	}
	parent[x] = find(parent[x]);
	return parent[x];
}

void unionx(int x, int y)
{
	int px = find(x);
	int py = find(y);
	if(rankx[px]>rankx[py])
	{
		parent[py] = px;
	}
	else if(rankx[px]<rankx[py])
	{
		parent[px] = py;
	}
	else
	{
		parent[px] = py;
		rankx[px]++;
	}
}

int main()
{
	ios_base::sync_with_stdio(false);
	int n;
	cin>>n;
	int x[n];
	int y[n];
	for(int i=0;i<n;i++)
	{
		int u,v;
		cin>>u>>v;
		x[i] = u;
		y[i] = v;
		parent[i]=i; //Changed from -1 to i
		rankx[i]=0;
	}
	for(int i=0;i<n;i++)
	{
		for(int j=i+1;j<n;j++)
		{
			if(x[i]==x[j] || y[i]==y[j])
			{
				unionx(i,j);
			}
		}
	}
	int ans =0;
	for(int i=0;i<n;i++)
	{
		if(parent[i]==-1)
		{
			ans++;
		}
	}
	cout<<ans-1;
	return 0;
}


#include <bits/stdc++.h>

//PROBLEM: http://codeforces.com/problemset/problem/217/A
/**
Uses DSU to find connected component
Note don't use names linke union and rank as they are ambiguous

**/

using namespace std;

int parent[1000001];
int rankx[1000001];
int find(int x)
{
	if(parent[x]==x)
	{
		return parent[x];
	}
	parent[x] = find(parent[x]);
	return parent[x];
}

void unionx(int x, int y)
{
	int px = find(x);
	int py = find(y);
	if(rankx[px]>rankx[py])
	{
		parent[py] = px;
	}
	else if(rankx[px]<rankx[py])
	{
		parent[px] = py;
	}
	else
	{
		parent[px] = py;
		rankx[px]++;
	}
}

int main()
{
	ios_base::sync_with_stdio(false);
	int n;
	cin>>n;
	int x[n];
	int y[n];
	for(int i=0;i<n;i++)
	{
		int u,v;
		cin>>u>>v;
		x[i] = u;
		y[i] = v;
		parent[i]=i; //Changed from -1 to i
		rankx[i]=0;
	}
	for(int i=0;i<n;i++)
	{
		for(int j=i+1;j<n;j++)
		{
			if(x[i]==x[j] || y[i]==y[j])
			{
				unionx(i,j);
			}
		}
	}
	int ans =0;
	for(int i=0;i<n;i++)
	{
		if(parent[i]==-1)
		{
			ans++;
		}
	}
	cout<<ans-1;
	return 0;
}


#include <bits/stdc++.h>

//PROBLEM: http://codeforces.com/problemset/problem/217/A
/**
Uses DSU to find connected component
Note don't use names linke union and rank as they are ambiguous

**/

using namespace std;

int parent[1000001];
int rankx[1000001];
int find(int x)
{
	if(parent[x]==x)
	{
		return x;
	}
	parent[x] = find(parent[x]);
	return parent[x];
}

void unionx(int x, int y)
{
	int px = find(x);
	int py = find(y);
	if(rankx[px]>rankx[py])
	{
		parent[py] = px;
	}
	else if(rankx[px]<rankx[py])
	{
		parent[px] = py;
	}
	else
	{
		parent[px] = py;
		rankx[px]++;
	}
}

int main()
{
	ios_base::sync_with_stdio(false);
	int n;
	cin>>n;
	int x[n];
	int y[n];
	for(int i=0;i<n;i++)
	{
		int u,v;
		cin>>u>>v;
		x[i] = u;
		y[i] = v;
		parent[i]=i; //Changed from -1 to i
		rankx[i]=0;
	}
	for(int i=0;i<n;i++)
	{
		for(int j=i+1;j<n;j++)
		{
			if(x[i]==x[j] || y[i]==y[j])
			{
				unionx(i,j);
			}
		}
	}
	int ans =0;
	for(int i=0;i<n;i++)
	{
		if(parent[i]==-1)
		{
			ans++;
		}
	}
	cout<<ans-1;
	return 0;
}


#include <bits/stdc++.h>

//PROBLEM: http://codeforces.com/problemset/problem/217/A
/**
Uses DSU to find connected component
Note don't use names linke union and rank as they are ambiguous

**/

using namespace std;

int parent[1000001];
int rankx[1000001];
int find(int x)
{
	if(parent[x]==x)
	{
		return parent[x];
	}
	parent[x] = find(parent[x]);
	return parent[x];
}

void unionx(int x, int y)
{
	int px = find(x);
	int py = find(y);
	if(rankx[px]>rankx[py])
	{
		parent[py] = px;
	}
	else if(rankx[px]<rankx[py])
	{
		parent[px] = py;
	}
	else
	{
		parent[px] = py;
		rankx[px]++;
	}
}

int main()
{
	ios_base::sync_with_stdio(false);
	int n;
	cin>>n;
	int x[n];
	int y[n];
	for(int i=0;i<n;i++)
	{
		int u,v;
		cin>>u>>v;
		x[i] = u;
		y[i] = v;
		parent[i]=i; //Changed from -1 to i
		rankx[i]=0;
	}
	for(int i=0;i<n;i++)
	{
		for(int j=i+1;j<n;j++)
		{
			if(x[i]==x[j] || y[i]==y[j])
			{
				unionx(i,j);
			}
		}
	}
	int ans =0;
	for(int i=0;i<n;i++)
	{
		if(parent[i]==i)
		{
			ans++;
		}
	}
	cout<<ans-1;
	return 0;
}


#include <bits/stdc++.h>

//PROBLEM: http://codeforces.com/problemset/problem/217/A
/**
Uses DSU to find connected component

**/

using namespace std;

int parent[1000001];
int rankx[1000001];
int find(int x)
{
	if(parent[x]==-1)
	{
		return x;
	}
	return find(parent[x]);
}

void unionx(int x, int y)
{
	int px = find(x);
	int py = find(y);
	if(rankx[px]>rankx[py])
	{
		parent[py] = px;
	}
	else if(rankx[px]<rankx[py])
	{
		parent[px] = py;
	}
	else
	{
		parent[px] = py;
		rankx[px]++;
	}
}

int main()
{
	ios_base::sync_with_stdio(false);
	int n;
	cin>>n;
	int x[n];
	int y[n];
	for(int i=0;i<n;i++)
	{
		int u,v;
		cin>>u>>v;
		x[i] = u;
		y[i] = v;
		parent[i]=-1;
		rankx[i]=0;
	}
	for(int i=0;i<n;i++)
	{
		for(int j=i+1;j<n;j++)
		{
			if(x[i]==x[j] || y[i]==y[j])
			{
				unionx(i,j);
			}
		}
	}
	int ans =0;
	for(int i=0;i<n;i++)
	{
		if(parent[i]==-1)
		{
			ans++;
		}
	}
	cout<<ans-1;
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int parent[1000001];

int find(int x)
{
	if(parent[x]==-1)
	{
		return x;
	}
	return find(parent[x]);
}

void unionx(int x, int y)
{
	int px = find(x);
	int py = find(y);
	if(px!=py)
	{
		parent[px] = py;
	}
}

int main()
{
	ios_base::sync_with_stdio(false);
	int n;
	cin>>n;
	int x[n];
	int y[n];
	for(int i=0;i<n;i++)
	{
		int u,v;
		cin>>u>>v;
		x[i] = u;
		y[i] = v;
		parent[i]=-1;
	}
	for(int i=0;i<n;i++)
	{
		for(int j=i+1;j<n;j++)
		{
			if(x[i]==x[j] || y[i]==y[j])
			{
				unionx(i,j);
			}
		}
	}
	int ans =0;
	for(int i=0;i<n;i++)
	{
		if(parent[i]==-1)
		{
			ans++;
		}
	}
	cout<<ans-1;
	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define ufo ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

const int N = 105;

int n,ans;
pair<int,int> a[200];
int parent[200];
int rnk[200];

void make_set(int v)
{
    parent[v] = v;
    rnk[v] = 0;
}

int find_set(int v)
{
    if(parent[v] == v)return v;
    else return parent[v] = find_set(v);
}

void union_sets(int a,int b)
{
    a = find_set(a);
    b = find_set(b);
    if(a != b){
        if(rnk[a] < rnk[b])
            swap(a,b);
        parent[b] = a;
        if(rnk[a] == rnk[b])
            rnk[a]++;
    }
}

main()
{
    ufo;
    cin>>n;
    for(int i = 0;i < n;i++){
        cin>>a[i].ff>>a[i].ss;
        make_set(i);
    }
    for(int i = 0;i < n;i++)
        for(int j = i+1;j < n;j++)
            if(a[i].ff == a[j].ff || a[i].ss == a[j].ss)
                if(find_set(i) != find_set(j)){
                    union_sets(i,j);
                    ans++;
                }
    cout<<n - ans - 1;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define ufo ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

const int N = 205;

int n,ans;
pair<int,int> a[N];
int parent[N];

int find_set(int v)
{
    if(parent[v] == v)return v;
    else return parent[v] = find_set(v);
}

void union_sets(int a,int b)
{
    a = find_set(a);
    b = find_set(b);
    if(a != b)
        parent[b] = a;
}

main()
{
    ufo;
    cin>>n;
    for(int i = 0;i < n;i++){
        cin>>a[i].ff>>a[i].ss;
        parent[i] = i;
    }
    for(int i = 0;i < n;i++)
        for(int j = i+1;j < n;j++)
            if(a[i].ff == a[j].ff || a[i].ss == a[j].ss)
                if(find_set(i) != find_set(j)){
                    union_sets(i,j);
                    ans++;
                }
    cout<<n - ans - 1;
}


#include<bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define ufo ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

const int N = 505;

int n,ans;
pair<int,int> a[N];
int parent[N];

int find_set(int v)
{
    if(parent[v] == v)return v;
    else return parent[v] = find_set(v);
}

void union_sets(int a,int b)
{
    a = find_set(a);
    b = find_set(b);
    if(a != b)
        parent[b] = a;
}

main()
{
    ufo;
    cin>>n;
    for(int i = 0;i < n;i++){
        cin>>a[i].ff>>a[i].ss;
        parent[i] = i;
    }
    for(int i = 0;i < n;i++)
        for(int j = i+1;j < n;j++)
            if(a[i].ff == a[j].ff || a[i].ss == a[j].ss)
                if(find_set(i) != find_set(j)){
                    union_sets(i,j);
                    ans++;
                }
    cout<<n - ans - 1;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define int long long
#define ff first
#define ss second
#define pb push_back
#define ufo ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

const int N = 1e3 + 5;

int n,ans;
pair<int,int> a[N];
int parent[N];
//int size[N];

void make_set(int v)
{
    parent[v] = v;
//    size[v] = 1;
}

int find_set(int v)
{
    if(parent[v] == v)return v;
    else return parent[v] = find_set(v);
}

void union_sets(int a,int b)
{
    a = find_set(a);
    b = find_set(b);
    if(a != b){
//        if(size[a] < size[b])
//            swap(a,b);
        parent[b] = a;
//        size[a] += size[b];
    }
}

main()
{
    ufo;
    cin>>n;
    for(int i = 0;i < n;i++){
        cin>>a[i].ff>>a[i].ss;
        parent[i] = i;
    }
    for(int i = 0;i < n;i++)
        for(int j = i+1;j < n;j++)
            if(a[i].ff == a[j].ff || a[i].ss == a[j].ss)
                if(find_set(i) != find_set(j)){
                    union_sets(i,j);
                    ans++;
                }
    cout<<n - ans - 1;
}


#include<bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define ufo ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

const int N = 105;

int n,ans;
pair<int,int> a[200];
int parent[200];
int rnk[200];

void make_set(int v)
{
    parent[v] = v;
    rnk[v] = 0;
}

int find_set(int v)
{
    if(parent[v] == v)return v;
    return parent[v] = find_set(parent[v]);
}

void union_sets(int a,int b)
{
    a = find_set(a);
    b = find_set(b);
    if(a != b){
        if(rnk[a] < rnk[b])
            swap(a,b);
        parent[b] = a;
        if(rnk[a] == rnk[b])
            rnk[a]++;
    }
}

main()
{
    ufo;
    cin>>n;
    for(int i = 0;i < n;i++){
        cin>>a[i].ff>>a[i].ss;
        make_set(i);
    }
    for(int i = 0;i < n;i++)
        for(int j = i+1;j < n;j++)
            if(a[i].ff == a[j].ff || a[i].ss == a[j].ss)
                if(find_set(i) != find_set(j)){
                    union_sets(i,j);
                    ans++;
                }
    cout<<n - ans - 1;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define int long long
#define ff first
#define ss second
#define pb push_back
#define ufo ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

const int N = 1e2 + 5;

int n,ans;
int a[N][2];
int parent[N];
int size[N];

void make_set(int v)
{
    parent[v] = v;
    size[v] = 1;
}

int find_set(int v)
{
    if(parent[v] == v)return v;
    else return parent[v] = find_set(v);
}

void union_sets(int a,int b)
{
    a = find_set(a);
    b = find_set(b);
    if(a != b){
        if(size[a] < size[b])
            swap(a,b);
        parent[b] = a;
        size[a] += size[b];
    }
}

main()
{
    ufo;
    cin>>n;
    for(int i = 0;i < n;i++){
        cin>>a[i][0]>>a[i][1];
        parent[i] = i;
    }
    for(int i = 0;i < n;i++)
        for(int j = i+1;j < n;j++)
            if(a[i][0] == a[j][0] || a[i][1] == a[j][1])
                if(find_set(i) != find_set(j)){
                    union_sets(i,j);
                    ans++;
                }
    cout<<n - ans - 1;
}


#include<bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define ufo ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

const int N  =  105;

int n,ans;
pair<int,int> a[200];
int parent[200];
int rnk[200];

void make_set(int v)
{
    parent[v] = v;
    rnk[v] = 0;
}

int find_set(int v)
{
    if(parent[v] == v)return v;
    return parent[v] = find_set(parent[v]);
}

void union_sets(int a,int b)
{
    a = find_set(a);
    b = find_set(b);
    if(a != b){
        if(rnk[a] < rnk[b])
            swap(a,b);
        parent[b] = a;
        if(rnk[a] == rnk[b])
            rnk[a]++;
    }
}

main()
{
    ufo;
    cin>>n;
    for(int i = 0;i < n;i++){
        cin>>a[i].ff>>a[i].ss;
        make_set(i);
    }
    for(int i = 0;i < n;i++)
        for(int j = i+1;j < n;j++)
            if(a[i].ff == a[j].ff || a[i].ss == a[j].ss)
                if(find_set(i) != find_set(j)){
                    union_sets(i,j);
                    ans++;
                }
    cout<<n - ans - 1;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define ufo ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

const int N = 105;

int n,ans;
pair<int,int> a[N];
int parent[N];
int rnk[N];

void make_set(int v)
{
    parent[v] = v;
    rnk[v] = 0;
}

int find_set(int v)
{
    if(parent[v] == v)return v;
    else return parent[v] = find_set(v);
}

void union_sets(int a,int b)
{
    a = find_set(a);
    b = find_set(b);
    if(a != b){
        if(rnk[a] < rnk[b])
            swap(a,b);
        parent[b] = a;
        if(rnk[a] == rnk[b])
            rnk[a]++;
    }
}

main()
{
    ufo;
    cin>>n;
    for(int i = 0;i < n;i++){
        cin>>a[i].ff>>a[i].ss;
        make_set(i);
    }
    for(int i = 0;i < n;i++)
        for(int j = i+1;j < n;j++)
            if(a[i].ff == a[j].ff || a[i].ss == a[j].ss)
                if(find_set(i) != find_set(j)){
                    union_sets(i,j);
                    ans++;
                }
    cout<<n - ans - 1;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define int long long
#define ff first
#define ss second
#define pb push_back
#define ufo ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

const int N = 1e3 + 5;

int n,ans;
int a[N][2];
int parent[N];
int size[N];

void make_set(int v)
{
    parent[v] = v;
    size[v] = 1;
}

int find_set(int v)
{
    if(parent[v] == v)return v;
    else return parent[v] = find_set(v);
}

void union_sets(int a,int b)
{
    a = find_set(a);
    b = find_set(b);
    if(a != b){
        if(size[a] < size[b])
            swap(a,b);
        parent[b] = a;
        size[a] += size[b];
    }
}

main()
{
    ufo;
    cin>>n;
    for(int i = 0;i < n;i++){
        cin>>a[i][0]>>a[i][1];
        parent[i] = i;
    }
    for(int i = 0;i < n;i++)
        for(int j = i+1;j < n;j++)
            if(a[i][0] == a[j][0] || a[i][1] == a[j][1])
                if(find_set(i) != find_set(j)){
                    union_sets(i,j);
                    ans++;
                }
    cout<<n - ans - 1;
}


#include<bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define ufo ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

const int N = 1005;

int n,ans;
pair<int,int> a[N];
int parent[N];

int find_set(int v)
{
    if(parent[v] == v)return v;
    else return parent[v] = find_set(v);
}

void union_sets(int a,int b)
{
    a = find_set(a);
    b = find_set(b);
    if(a != b)
        parent[b] = a;
}

main()
{
    ufo;
    cin>>n;
    for(int i = 0;i < n;i++){
        cin>>a[i].ff>>a[i].ss;
        parent[i] = i;
    }
    for(int i = 0;i < n;i++)
        for(int j = i+1;j < n;j++)
            if(a[i].ff == a[j].ff || a[i].ss == a[j].ss)
                if(find_set(i) != find_set(j)){
                    union_sets(i,j);
                    ans++;
                }
    cout<<n - ans - 1;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define ufo ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

const int N = 105;

int n,ans;
pair<int,int> a[N];
int parent[N];
int size[N];

void make_set(int v)
{
    parent[v] = v;
    size[v] = 1;
}

int find_set(int v)
{
    if(parent[v] == v)return v;
    else return parent[v] = find_set(v);
}

void union_sets(int a,int b)
{
    a = find_set(a);
    b = find_set(b);
    if(a != b){
        if(size[a] < size[b])
            swap(a,b);
        parent[b] = a;
        size[a] += size[b];
    }
}

main()
{
    ufo;
    cin>>n;
    for(int i = 0;i < n;i++){
        cin>>a[i].ff>>a[i].ss;
        make_set(i);
    }
    for(int i = 0;i < n;i++)
        for(int j = i+1;j < n;j++)
            if(a[i].ff == a[j].ff || a[i].ss == a[j].ss)
                if(find_set(i) != find_set(j)){
                    union_sets(i,j);
                    ans++;
                }
    cout<<n - ans - 1;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ff first
#define ss second
#define pb push_back
#define ufo ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

const int N = 105;

int n,ans;
pair<int,int> a[200];
int parent[200];
int rnk[200];

void make_set(int v)
{
    parent[v] = v;
    rnk[v] = 0;
}

int find_set(int v)
{
    if(parent[v] == v)return v;
    return parent[v] = find_set(v);
}

void union_sets(int a,int b)
{
    a = find_set(a);
    b = find_set(b);
    if(a != b){
        if(rnk[a] < rnk[b])
            swap(a,b);
        parent[b] = a;
        if(rnk[a] == rnk[b])
            rnk[a]++;
    }
}

main()
{
    ufo;
    cin>>n;
    for(int i = 0;i < n;i++){
        cin>>a[i].ff>>a[i].ss;
        make_set(i);
    }
    for(int i = 0;i < n;i++)
        for(int j = i+1;j < n;j++)
            if(a[i].ff == a[j].ff || a[i].ss == a[j].ss)
                if(find_set(i) != find_set(j)){
                    union_sets(i,j);
                    ans++;
                }
    cout<<n - ans - 1;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define int long long
#define ff first
#define ss second
#define pb push_back
#define ufo ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

const int N = 15 * 1e4 + 5;

int n,ans;
int a[N][2];
int parent[N];
int size[N];

void make_set(int v)
{
    parent[v] = v;
    size[v] = 1;
}

int find_set(int v)
{
    if(parent[v] == v)return v;
    else return parent[v] = find_set(v);
}

void union_sets(int a,int b)
{
    a = find_set(a);
    b = find_set(b);
    if(a != b){
        if(size[a] < size[b])
            swap(a,b);
        parent[b] = a;
        size[a] += size[b];
    }
}

main()
{
    ufo;
    cin>>n;
    for(int i = 0;i < n;i++){
        cin>>a[i][0]>>a[i][1];
        parent[i] = i;
    }
    for(int i = 0;i < n;i++)
        for(int j = i+1;j < n;j++)
            if(a[i][0] == a[j][0] || a[i][1] == a[j][1])
                if(find_set(i) != find_set(j)){
                    union_sets(i,j);
                    ans++;
                }
    cout<<n - ans - 1;
}


#include<bits/stdc++.h>
#define ll long long
#define int long long
#define ff first
#define ss second
#define pb push_back
#define ufo ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

const int N = 205;

int n,ans;
pair<int,int> a[N];
int parent[N];
int size[N];

void make_set(int v)
{
    parent[v] = v;
    size[v] = 1;
}

int find_set(int v)
{
    if(parent[v] == v)return v;
    else return parent[v] = find_set(v);
}

void union_sets(int a,int b)
{
    a = find_set(a);
    b = find_set(b);
    if(a != b){
        if(size[a] < size[b])
            swap(a,b);
        parent[b] = a;
        size[a] += size[b];
    }
}

main()
{
    ufo;
    cin>>n;
    for(int i = 0;i < n;i++){
        cin>>a[i].ff>>a[i].ss;
        parent[i] = i;
    }
    for(int i = 0;i < n;i++)
        for(int j = i+1;j < n;j++)
            if(a[i].ff == a[j].ff || a[i].ss == a[j].ss)
                if(find_set(i) != find_set(j)){
                    union_sets(i,j);
                    ans++;
                }
    cout<<n - ans - 1;
}


#include<bits/stdc++.h>
#define ll long long
#define int long long
#define ff first
#define ss second
#define pb push_back
#define ufo ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

const int N = 105;

int n,ans;
pair<int,int> a[N];
int parent[N];

int find_set(int v)
{
    if(parent[v] == v)return v;
    else return parent[v] = find_set(v);
}

void union_sets(int a,int b)
{
    a = find_set(a);
    b = find_set(b);
    if(a != b)
        parent[b] = a;
}

main()
{
    ufo;
    cin>>n;
    for(int i = 0;i < n;i++){
        cin>>a[i].ff>>a[i].ss;
        parent[i] = i;
    }
    for(int i = 0;i < n;i++)
        for(int j = i+1;j < n;j++)
            if(a[i].ff == a[j].ff || a[i].ss == a[j].ss)
                if(find_set(i) != find_set(j)){
                    union_sets(i,j);
                    ans++;
                }
    cout<<n - ans - 1;
}


#include <bits/stdc++.h>

#include <time.h>
#include <random>

using namespace std;

#define vec vector
#define ALL(x) (x).begin(), (x).end()
#define mp make_pair

typedef pair< int, int > pii;
typedef long long ll;

int const inf = 1000 * 1000 * 1000;
ll const inf64 = 1ll * inf * inf;

bool check(ll a, ll b , ll c) {
    if(a <= 0 || b <= 0 || c <= 0) return 0;
    if(a > b) swap(a, b);
    if(a > c) swap(a, c);
    if(b > c) swap(b, c);
    return a * a + b * b == c * c;
}

ll is_sqrt(ll x) {
    ll t = sqrt(x);
    while((t + 1) * (t + 1) <= x) t++;
    if(t * t == x) return t;
    else return -1;
}

bool solve() {

    ll a;

    cin >> a;

    for(ll b, c, d = -5000000;d <= 5000000;d++) {
        b = a + d;
        if(b <= 0) continue;
        c = is_sqrt(a * a + b * b);
        if(c != -1) {
            cout << b << " " << c << "\n";
            return true;
        }
        if(a != b) {
            c = is_sqrt(abs(a * a - b * b));
            if(c != -1) {
                cout << b << " " << c << "\n";
                return true;
            }
        }
    }

    for(ll c, b = 1;b <= 15000000;b++) {
        c = is_sqrt(a * a + b * b);
        if(c != -1) {
            cout << b << " " << c << "\n";
            return true;
        }
        if(a != b) {
            c = is_sqrt(abs(a * a - b * b));
            if(c != -1) {
                cout << b << " " << c << "\n";
                return true;
            }
        }
    }

    if(a % 2 == 0) {
        if(check(a, 1 + (a / 2) * (a / 2), (a / 2) * (a / 2) - 1)) {
            cout << 1 + (a / 2) * (a / 2) << " " << (a / 2) * (a / 2) - 1 << "\n";
            return true;
        }
    }

    for(ll n, m = 1;m * m < a;m++) {
        n = is_sqrt(a - m * m);
        if(n != -1) {
            if(check(a, abs(m * m - n * n), 2 * m * n)) {
                cout << abs(m * m - n * n) << " " << 2 * m * n << "\n";
                return true;
            }
        }
    }

    for(ll m, n = 1;n <= 15000000;n++) {
        m = is_sqrt(n * n + a);
        if(m != -1) {
            if(check(a, m * m + n * n, 2 * m * n)) {
                cout << m * m + n * n << " " << 2 * m * n << "\n";
                return true;
            }
        }
    }

    cout << "-1\n";

    return true;
}

int main() {

    //while(solve());
    solve();

    return 0;
}


#include <bits/stdc++.h>

#include <time.h>
#include <random>

using namespace std;

#define vec vector
#define ALL(x) (x).begin(), (x).end()
#define mp make_pair

typedef pair< int, int > pii;
typedef long long ll;

int const inf = 1000 * 1000 * 1000;
ll const inf64 = 1ll * inf * inf;

bool check(ll a, ll b , ll c) {
    if(a <= 0 || b <= 0 || c <= 0) return 0;
    if(a > b) swap(a, b);
    if(a > c) swap(a, c);
    if(b > c) swap(b, c);
    return a * a + b * b == c * c;
}

ll is_sqrt(ll x) {
    ll t = sqrt(x);
    while((t + 1) * (t + 1) <= x) t++;
    if(t * t == x) return t;
    else return -1;
}

bool solve() {

    ll a;

    cin >> a;

    for(ll c, b = 1;b <= 20000000;b++) {
        c = is_sqrt(a * a + b * b);
        if(c != -1) {
            cout << b << " " << c << "\n";
            return true;
        }
        if(a != b) {
            c = is_sqrt(abs(a * a - b * b));
            if(c != -1) {
                cout << b << " " << c << "\n";
                return true;
            }
        }
    }

    if(a % 2 == 0) {
        if(check(a, 1 + (a / 2) * (a / 2), (a / 2) * (a / 2) - 1)) {
            cout << 1 + (a / 2) * (a / 2) << " " << (a / 2) * (a / 2) - 1 << "\n";
            return true;
        }
    }

    for(ll n, m = 1;m * m < a;m++) {
        n = is_sqrt(a - m * m);
        if(n != -1) {
            if(check(a, abs(m * m - n * n), 2 * m * n)) {
                cout << abs(m * m - n * n) << " " << 2 * m * n << "\n";
                return true;
            }
        }
    }

    for(ll m, n = 1;n <= 20000000;n++) {
        m = is_sqrt(n * n + a);
        if(m != -1) {
            if(check(a, m * m + n * n, 2 * m * n)) {
                cout << m * m + n * n << " " << 2 * m * n << "\n";
                return true;
            }
        }
    }

    cout << "-1\n";

    return true;
}

int main() {

    //while(solve());
    solve();

    return 0;
}


#include <bits/stdc++.h>

#include <time.h>
#include <random>

using namespace std;

#define vec vector
#define ALL(x) (x).begin(), (x).end()
#define mp make_pair

typedef pair< int, int > pii;
typedef long long ll;

int const inf = 1000 * 1000 * 1000;
ll const inf64 = 1ll * inf * inf;

bool check(ll a, ll b , ll c) {
    if(a <= 0 || b <= 0 || c <= 0) return 0;
    if(a > b) swap(a, b);
    if(a > c) swap(a, c);
    if(b > c) swap(b, c);
    return a * a + b * b == c * c;
}

ll is_sqrt(ll x) {
    ll t = sqrt(x);
    while((t + 1) * (t + 1) <= x) t++;
    if(t * t == x) return t;
    else return -1;
}

bool solve() {

    ll a;

    cin >> a;

    for(ll b, c, d = -5000000;d <= 5000000;d++) {
        b = a + d;
        c = is_sqrt(a * a + b * b);
        if(c != -1) {
            cout << b << " " << c << "\n";
            return true;
        }
        if(a != b) {
            c = is_sqrt(abs(a * a - b * b));
            if(c != -1) {
                cout << b << " " << c << "\n";
                return true;
            }
        }
    }

    for(ll c, b = 1;b <= 15000000;b++) {
        c = is_sqrt(a * a + b * b);
        if(c != -1) {
            cout << b << " " << c << "\n";
            return true;
        }
        if(a != b) {
            c = is_sqrt(abs(a * a - b * b));
            if(c != -1) {
                cout << b << " " << c << "\n";
                return true;
            }
        }
    }

    if(a % 2 == 0) {
        if(check(a, 1 + (a / 2) * (a / 2), (a / 2) * (a / 2) - 1)) {
            cout << 1 + (a / 2) * (a / 2) << " " << (a / 2) * (a / 2) - 1 << "\n";
            return true;
        }
    }

    for(ll n, m = 1;m * m < a;m++) {
        n = is_sqrt(a - m * m);
        if(n != -1) {
            if(check(a, abs(m * m - n * n), 2 * m * n)) {
                cout << abs(m * m - n * n) << " " << 2 * m * n << "\n";
                return true;
            }
        }
    }

    for(ll m, n = 1;n <= 15000000;n++) {
        m = is_sqrt(n * n + a);
        if(m != -1) {
            if(check(a, m * m + n * n, 2 * m * n)) {
                cout << m * m + n * n << " " << 2 * m * n << "\n";
                return true;
            }
        }
    }

    cout << "-1\n";

    return true;
}

int main() {

    //while(solve());
    solve();

    return 0;
}


#include <bits/stdc++.h>

#include <time.h>
#include <random>

using namespace std;

#define vec vector
#define ALL(x) (x).begin(), (x).end()
#define mp make_pair

typedef pair< int, int > pii;
typedef long long ll;

int const inf = 1000 * 1000 * 1000;
ll const inf64 = 1ll * inf * inf;

bool check(ll a, ll b, ll c) {
    return a > 0 && b > 0 && c > 0 &&
            (a * a + b * b == c * c ||
             (a * a + c * c == b * b) ||
             (b * b + c * c == a * a));
}

bool solve() {

    ll a;
    cin >> a;

    if(a <= 2) {
        cout << "-1\n";
        return true;
    }

    if(a % 2) {
        ll m = a / 2;
        ll b, c;
        b = 2 * m * m + 2 * m;
        c = 2 * m * m + 2 * m + 1;
        if(check(a, b, c)) {
            cout << b << " " << c << "\n";
            return true;
        }
    }else {
        ll m = a / 2;
        ll n = 1;
        ll b, c;
        b = m * m - n * n;
        c = m * m + n * n;
        if(check(a, b, c)) {
            cout << b << ' ' << c << '\n';
            return true;
        }
    }

    cout << "-1\n";

    return true;
}

int main() {

    //while(solve());
    solve();

    return 0;
}


#include <bits/stdc++.h>

#include <time.h>
#include <random>

using namespace std;

#define vec vector
#define ALL(x) (x).begin(), (x).end()
#define mp make_pair

typedef pair< int, int > pii;
typedef long long ll;

int const inf = 1000 * 1000 * 1000;
ll const inf64 = 1ll * inf * inf;

bool check(ll a, ll b , ll c) {
    if(a <= 0 || b <= 0 || c <= 0) return 0;
    if(a > b) swap(a, b);
    if(a > c) swap(a, c);
    if(b > c) swap(b, c);
    return a * a + b * b == c * c;
}

ll is_sqrt(ll x) {
    ll t = sqrt(x);
    while((t + 1) * (t + 1) <= x) t++;
    if(t * t == x) return t;
    else return -1;
}

bool solve() {

    ll a;

    cin >> a;

    for(ll b, c, d = -500000;d <= 500000;d++) {
        b = a + d;
        if(b <= 0) continue;
        c = is_sqrt(a * a + b * b);
        if(c != -1) {
            cout << b << " " << c << "\n";
            return true;
        }
        if(a != b) {
            c = is_sqrt(abs(a * a - b * b));
            if(c != -1) {
                cout << b << " " << c << "\n";
                return true;
            }
        }
    }

    for(ll c, b = 1;b <= 10000000;b++) {
        c = is_sqrt(a * a + b * b);
        if(c != -1) {
            cout << b << " " << c << "\n";
            return true;
        }
        if(a != b) {
            c = is_sqrt(abs(a * a - b * b));
            if(c != -1) {
                cout << b << " " << c << "\n";
                return true;
            }
        }
    }

    if(a % 2 == 0) {
        if(check(a, 1 + (a / 2) * (a / 2), (a / 2) * (a / 2) - 1)) {
            cout << 1 + (a / 2) * (a / 2) << " " << (a / 2) * (a / 2) - 1 << "\n";
            return true;
        }
    }

    for(ll n, m = 1;m * m < a;m++) {
        n = is_sqrt(a - m * m);
        if(n != -1) {
            if(check(a, abs(m * m - n * n), 2 * m * n)) {
                cout << abs(m * m - n * n) << " " << 2 * m * n << "\n";
                return true;
            }
        }
    }

    for(ll m, n = 1;n <= 10000000;n++) {
        m = is_sqrt(n * n + a);
        if(m != -1) {
            if(check(a, m * m + n * n, 2 * m * n)) {
                cout << m * m + n * n << " " << 2 * m * n << "\n";
                return true;
            }
        }
    }

    cout << "-1\n";

    return true;
}

int main() {

    //while(solve());
    solve();

    return 0;
}


#include <bits/stdc++.h>

#include <time.h>
#include <random>

using namespace std;

#define vec vector
#define ALL(x) (x).begin(), (x).end()
#define mp make_pair

typedef pair< int, int > pii;
typedef long long ll;

int const inf = 1000 * 1000 * 1000;
ll const inf64 = 1ll * inf * inf;

bool solve() {

    int a;

    cin >> a;

    if(a <= 2) {
        cout << "-1\n";
        return true;
    }

    if(a % 2 == 0) {
        int n = 1;
        int m = a / 2;
        cout << m * m + n * n << ' ' << m * m - n * n << '\n';
        return true;
    }

    set< int > q;
    for(int x = 1;x * x <= a;x++) q.insert(x * x);

    for(int n, m, x, y = 1;y * y < a;y++) {
        x = a - y * y;
        if(!q.count(x)) continue;
        n = y;
        m = sqrt(x);
        if(n == m) continue;
        if(n > m) swap(n, m);
        cout << m * m - n * n << " " << 2 * m * n << "\n";
        return true;
    }

    for(int n = 1;n <= 10000000;n++) {
        ll m = 1ll * n * n + a;
        if(ll(sqrt(m)) * ll(sqrt(m)) != m) continue;
        m = sqrt(m);
        cout << m * m + 1ll * n * n << " " << 2ll * m * n << "\n";
        return true;
    }

    for(int b = 1;b <= 10000000;b++) {
        ll c = 1ll * b * b + 1ll * a * a;
        ll tmp = sqrt(c);
        if(tmp * tmp == c) {
            c = tmp;
            cout << b << " " << c << "\n";
            return true;
        }
        if(a == b) continue;
        c = abs(1ll * b * b - 1ll * a * a);
        tmp = sqrt(c);
        if(tmp * tmp == c) {
            c = tmp;
            cout << b << " " << c << "\n";
            return true;
        }
    }

    cout << "-1\n";

    return true;
}

int main() {

    //while(solve());
    solve();

    return 0;
}


#include <bits/stdc++.h>

#include <time.h>
#include <random>

using namespace std;

#define vec vector
#define ALL(x) (x).begin(), (x).end()
#define mp make_pair

typedef pair< int, int > pii;
typedef long long ll;

int const inf = 1000 * 1000 * 1000;
ll const inf64 = 1ll * inf * inf;

bool check(ll a, ll b , ll c) {
    if(a <= 0 || b <= 0 || c <= 0) return 0;
    if(a > b) swap(a, b);
    if(a > c) swap(a, c);
    if(b > c) swap(b, c);
    return a * a + b * b == c * c;
}

ll is_sqrt(ll x) {
    ll t = sqrt(x);
    while((t + 1) * (t + 1) <= x) t++;
    if(t * t == x) return t;
    else return -1;
}

bool solve() {

    ll a;

    cin >> a;

    for(ll c, b = 1;b <= 10000000;b++) {
        c = is_sqrt(a * a + b * b);
        if(c != -1) {
            cout << b << " " << c << "\n";
            return true;
        }
        if(a != b) {
            c = is_sqrt(abs(a * a - b * b));
            if(c != -1) {
                cout << b << " " << c << "\n";
                return true;
            }
        }
    }

    if(a % 2 == 0) {
        if(check(a, 1 + (a / 2) * (a / 2), (a / 2) * (a / 2) - 1)) {
            cout << 1 + (a / 2) * (a / 2) << " " << (a / 2) * (a / 2) - 1 << "\n";
            return true;
        }
    }

    for(ll n, m = 1;m * m < a;m++) {
        n = is_sqrt(a - m * m);
        if(n != -1) {
            if(check(a, abs(m * m - n * n), 2 * m * n)) {
                cout << abs(m * m - n * n) << " " << 2 * m * n << "\n";
                return true;
            }
        }
    }

    cout << "-1\n";

    return true;
}

int main() {

    //while(solve());
    solve();

    return 0;
}


#include <bits/stdc++.h>

#include <time.h>
#include <random>

using namespace std;

#define vec vector
#define ALL(x) (x).begin(), (x).end()
#define mp make_pair

typedef pair< int, int > pii;
typedef long long ll;

int const inf = 1000 * 1000 * 1000;
ll const inf64 = 1ll * inf * inf;

bool check(ll a, ll b , ll c) {
    if(a <= 0 || b <= 0 || c <= 0) return 0;
    if(a > b) swap(a, b);
    if(a > c) swap(a, c);
    if(b > c) swap(b, c);
    return a * a + b * b == c * c;
}

ll is_sqrt(ll x) {
    ll t = sqrt(x);
    while((t + 1) * (t + 1) <= x) t++;
    if(t * t == x) return t;
    else return -1;
}

pair< ll, ll > get(ll a) {
    for(ll c, b = 1;b <= 1000000;b++) {
        c = is_sqrt(a * a + b * b);
        if(c != -1) {
            //cout << b << " " << c << "\n";
            return mp(b, c);
        }
        if(a != b) {
            c = is_sqrt(abs(a * a - b * b));
            if(c != -1) {
                //cout << b << " " << c << "\n";
                return mp(b, c);
            }
        }
    }

    if(a % 2 == 0) {
        if(check(a, 1 + (a / 2) * (a / 2), (a / 2) * (a / 2) - 1)) {
            //cout << 1 + (a / 2) * (a / 2) << " " << (a / 2) * (a / 2) - 1 << "\n";
            return mp(1 + (a / 2) * (a / 2), (a / 2) * (a / 2) - 1);
        }
    }

    for(ll n, m = 1;m * m < a;m++) {
        n = is_sqrt(a - m * m);
        if(n != -1) {
            if(check(a, abs(m * m - n * n), 2 * m * n)) {
                //cout << abs(m * m - n * n) << " " << 2 * m * n << "\n";
                return mp(ll(abs(m * m - n * n)), 2 * m * n);
            }
        }
    }

    for(ll m, n = 1;n <= 1000000;n++) {
        m = is_sqrt(n * n + a);
        if(m != -1) {
            if(check(a, m * m + n * n, 2 * m * n)) {
                //cout << m * m + n * n << " " << 2 * m * n << "\n";
                return mp(m * m + n * n, 2 * m * n);
            }
        }
    }

    return mp(-1, -1);
}

bool solve() {

    ll a, tmp;

    cin >> a;

    tmp = a;

    set< ll > q;

    for(ll x = 2;x * x <= a;x++) {
        if(a % x) continue;
        q.insert(x);
        while(a % x == 0) a /= x;
    }

    if(a > 1) q.insert(a);

    a = tmp;

    for(ll x : q) {
        //cout << x << "\n";
        pair< ll, ll > t = get(x);
        if(t.first != -1) {
            cout << t.first * (a / x) << " " << t.second * (a / x) << "\n";
            return true;
        }
    }

    cout << "-1\n";

    return true;
}

int main() {

    //while(solve());
    solve();

    return 0;
}


#include <bits/stdc++.h>

#include <time.h>
#include <random>

using namespace std;

#define vec vector
#define ALL(x) (x).begin(), (x).end()
#define mp make_pair

typedef pair< int, int > pii;
typedef long long ll;

int const inf = 1000 * 1000 * 1000;
ll const inf64 = 1ll * inf * inf;

bool check(ll a, ll b , ll c) {
    if(a <= 0 || b <= 0 || c <= 0) return 0;
    if(a > b) swap(a, b);
    if(a > c) swap(a, c);
    if(b > c) swap(b, c);
    return a * a + b * b == c * c;
}

ll is_sqrt(ll x) {
    ll t = sqrt(x);
    while((t + 1) * (t + 1) <= x) t++;
    if(t * t == x) return t;
    else return -1;
}

pair< ll, ll > get(ll a) {
    for(ll c, b = 1;b <= 5000000;b++) {
        c = is_sqrt(a * a + b * b);
        if(c != -1) {
            //cout << b << " " << c << "\n";
            return mp(b, c);
            //return true;
        }
        if(a != b) {
            c = is_sqrt(abs(a * a - b * b));
            if(c != -1) {
                //cout << b << " " << c << "\n";
                return mp(b, c);
                //return true;
            }
        }
    }

    if(a % 2 == 0) {
        if(check(a, 1 + (a / 2) * (a / 2), (a / 2) * (a / 2) - 1)) {
            //cout << 1 + (a / 2) * (a / 2) << " " << (a / 2) * (a / 2) - 1 << "\n";
            return mp(1 + (a / 2) * (a / 2), (a / 2) * (a / 2) - 1);
            //return true;
        }
    }

    for(ll n, m = 1;m * m < a;m++) {
        n = is_sqrt(a - m * m);
        if(n != -1) {
            if(check(a, abs(m * m - n * n), 2 * m * n)) {
                //cout << abs(m * m - n * n) << " " << 2 * m * n << "\n";
                ll b, c;
                b = abs(m * m - n * n);
                c = 2 * m * n;
                return mp(b, c);
                //return mp(1ll * abs(m * m - n * n), 2ll * m * n);
            }
        }
    }

    return mp(-1, -1);
}

bool solve() {

    ll a, tmp;

    cin >> a;

    tmp = a;

    set< ll > q;

    for(ll x = 3;x * x <= a;x++) {
        if(a % x) continue;
        q.insert(x);
        while(a % x == 0) a /= x;
    }

    a = tmp;

    for(ll x : q) {
        pair< ll, ll > t = get(x);
        if(t.first != -1) {
            cout << t.first * (a / x) << " " << t.second * (a / x) << "\n";
            return true;
        }
    }

    cout << -1 << "\n";

    return true;
}

int main() {

    //while(solve());
    solve();

    return 0;
}