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

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

Input

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

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

Output

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

Examples

Input

2
2 1
1 2


Output

1


Input

2
2 1
4 1


Output

0
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.Scanner;
import java.util.TreeMap;

public class Solution{
	
	///////////////////////////////////////////////////////////////////////////
	static class FastScanner{
		BufferedReader s;
		StringTokenizer st;
		
		public FastScanner(){
			st = new StringTokenizer("");
			s = new BufferedReader(new InputStreamReader(System.in));
		}
		
		public FastScanner(File f) throws FileNotFoundException{
			st = new StringTokenizer("");
			s = new BufferedReader (new FileReader(f));
		}
		
		public int nextInt() throws IOException{
			if(st.hasMoreTokens())
				return Integer.parseInt(st.nextToken());
			else{
				st = new StringTokenizer(s.readLine());
				return nextInt();
			}
		}
		
		public double nextDouble() throws IOException{
			if(st.hasMoreTokens())
				return Double.parseDouble(st.nextToken());
			else{
				st = new StringTokenizer(s.readLine());
				return nextDouble();
			}
		}
		
		public long nextLong() throws IOException{
			if(st.hasMoreTokens())
				return Long.parseLong(st.nextToken());
			else{
				st = new StringTokenizer(s.readLine());
				return nextLong();
			}
		}
		
		public String nextString() throws IOException{
			if(st.hasMoreTokens())
				return st.nextToken();
			else{
				st = new StringTokenizer(s.readLine());
				return nextString();
			}
			
		}
		public String readLine() throws IOException{
			return s.readLine();
		}
		
		public void close() throws IOException{
			s.close();
		}
		
	}
	
	////////////////////////////////////////////////////////////////////
	//		Number Theory
	
	public static int divisor(int x){
	    int limit = x;
	    int numberOfDivisors = 0;

	    for (int i=1; i < limit; ++i) {
	        if (x % i == 0) {
	            limit = x / i;
	            if (limit != i) {
	                numberOfDivisors++;
	            }
	            numberOfDivisors++;
	        }
	    }

	    return numberOfDivisors;
    }
	
	
	public static long gcd(long a, long b){
		return b == 0 ? a : gcd(b,a%b);
	}
	
  
	public static long lcm(long a,long b, long c){
		return lcm(lcm(a,b),c);
	}
	
	public static long lcm(long a, long b){
		return a*b/gcd(a,b);
	}
	
	
	////////////////////////////////////////////////////////////////////
	
	
//	  private static  	FastScanner s = new FastScanner(new File("input.txt"));
//	  private static PrintWriter ww = new PrintWriter(new FileWriter("output.txt"));
	  private static FastScanner s = new FastScanner();   
      private static PrintWriter ww = new PrintWriter(new OutputStreamWriter(System.out));
//    private static Scanner s = new Scanner(System.in);
      @SuppressWarnings("unused")
	private static Map<Integer,Integer> mm = new HashMap<Integer,Integer>();
      @SuppressWarnings("unused")
	private static Map<Integer,node> mn = new HashMap<Integer,node>();
//	private static ArrayList<Integer>[] arr = new ArrayList[2002];
//	private static boolean[] vis = new boolean[2002];
	private static int[] arr = new int[2002];
      
    //////////////////////////////////////////////////////////////////// 
      
     static class node{
      	int l;
      	int r;
      	int d;
      	public node(int x, int y, int z){
      		this.l  = x;
      		this.r = y;
      		this.d = z;
      	}
      }
    
    public static void union(int x, int y){
    	
    	x = find(x);
    	y = find(y);
    	arr[y] = x;
    }
    
    public static int find(int x){
    	
    	if(x == arr[x]) return x;
    	return find(arr[x]);
   
    }
	public static void main(String[] args) throws IOException{
	
	    int n = s.nextInt();
	    int[] xx = new int[2002];
	    int[] yy = new int[2002];
	    Set<Integer> st = new HashSet<Integer>();
	    Arrays.fill(arr, -1);
	    
	    for(int i=0;i<n;i++){
	    	xx[i] = s.nextInt();
	    	yy[i] = s.nextInt();
	    	arr[xx[i]] = xx[i];
	    	arr[1000+yy[i]] = 1000+yy[i];
	    }
	    
	    for(int i=0;i<n;i++)
	    	union(xx[i],1000+yy[i]);
	    
	    for(int i=0;i<2002;i++){
	    	if(arr[i] != -1){
	    		st.add(find(i));
	    	}
	    		
	    }
	    
	    ww.println(st.size()-1);
	    s.close();
	    ww.close();
	}
}
