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

#DSU0000022. Các thành phần trên lưới lục giác (Do use hexagon grid)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Các thành phần trên lưới lục giác (Do use hexagon grid)

Do use hexagon grid

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

Consider an infinite hexagonal grid. Every cell is identified by a pair of integer coordinates (x,y)(x,y). Unlike a square grid, a cell has exactly six neighboring cells at (x−1,y−1)(x-1,y-1), (x−1,y)(x-1,y), (x,y−1)(x,y-1), (x,y+1)(x,y+1), (x+1,y)(x+1,y) and (x+1,y+1)(x+1,y+1).

Exactly nn cells at distinct coordinates are black; all other cells are white. Two black cells belong to the same region if one can travel between them through a sequence of black cells, with consecutive cells adjacent in one of the six specified directions. Traveling through white cells is not allowed.

Count the connected regions of black cells. An isolated black cell forms a region on its own.

Input

The first line contains nn. Each of the following nn lines contains coordinates xi,yix_i,y_i of a black cell.

Output

Print the number of connected components.

Subtasks

  • Subtask 1 (20 points): 1≤n≤201\le n\le20; −1000≤xi,yi≤1000-1000\le x_i,y_i\le1000; no coordinate pair repeats.
  • Subtask 2 (30 points): 1≤n≤2001\le n\le200; −1000≤xi,yi≤1000-1000\le x_i,y_i\le1000; no coordinate pair repeats.
  • Subtask 3 (50 points): 1≤n≤10001\le n\le1000; −1000≤xi,yi≤1000-1000\le x_i,y_i\le1000; no coordinate pair repeats.

Examples

Input

6
-1 -1
0 1
0 2
1 0
1 2
2 0

Output

3

Explanation

Cell (−1,−1)(-1,-1) is isolated; (1,0),(2,0)(1,0),(2,0) form another component; and (0,1),(0,2),(1,2)(0,1),(0,2),(1,2) form the third.