PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#DSU0000016. Trượt băng (Ice Skating)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Trượt băng (Ice Skating)

Ice Skating

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn snow piles at distinct integer coordinates on an ice field. You can skate directly between two piles if they share the same xx-coordinate or the same yy-coordinate. You may add new piles at integer coordinates.

Find the minimum number of new piles needed so that every pile can be reached from every other pile.

Input

The first line contains an integer nn. Each of the next nn lines contains two integers xi,yix_i,y_i, the coordinates of a snow pile.

Output

Print the minimum number of new piles required.

Subtasks

  • Subtask 1 (20 points): 1≤n≤181\le n\le 18; 1≤xi,yi≤10001\le x_i,y_i\le 1000; all positions are distinct.
  • Subtask 2 (30 points): 1≤n≤701\le n\le 70; 1≤xi,yi≤10001\le x_i,y_i\le 1000; all positions are distinct.
  • Subtask 3 (50 points): 1≤n≤1001\le n\le 100; 1≤xi,yi≤10001\le x_i,y_i\le 1000; all positions are distinct.

Examples

Input

2
2 1
1 2

Output

1

Explanation

The two drifts share neither a row nor a column. Adding (2,2) connects both, so the answer is 1.