atcoder#ABC269D. [ABC269D] Do use hexagon grid

[ABC269D] Do use hexagon grid

Score : 400400 points

Problem Statement

We have an infinite hexagonal grid shown below. Initially, all squares are white.

A hexagonal cell is represented as (i,j)(i,j) with two integers ii and jj. Cell (i,j)(i,j) is adjacent to the following six cells:

  • (i1,j1)(i-1,j-1)
  • (i1,j)(i-1,j)
  • (i,j1)(i,j-1)
  • (i,j+1)(i,j+1)
  • (i+1,j)(i+1,j)
  • (i+1,j+1)(i+1,j+1)

Takahashi has painted NN cells (X1,Y1),(X2,Y2),,(XN,YN)(X_1,Y_1),(X_2,Y_2),\dots,(X_N,Y_N) black. Find the number of connected components formed by the black cells. Two black cells belong to the same connected component when one can travel between those two black cells by repeatedly moving to an adjacent black cell.

Constraints

  • All values in the input are integers.
  • 1N10001 \le N \le 1000
  • Xi,Yi1000|X_i|,|Y_i| \le 1000
  • The pairs (Xi,Yi)(X_i,Y_i) are distinct.

Input

The input is given from Standard Input in the following format:

NN

X1X_1 Y1Y_1

X2X_2 Y2Y_2

\vdots

XNX_N YNY_N

Output

Print the answer as an integer.

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

After Takahashi paints cells black, the grid looks as shown below.

The black squares form the following three connected components:

  • (1,1)(-1,-1)
  • (1,0),(2,0)(1,0),(2,0)
  • (0,1),(0,2),(1,2)(0,1),(0,2),(1,2)
4
5 0
4 1
-3 -4
-2 -5
4
5
2 1
2 -1
1 0
3 1
1 -1
1