P5816 [CQOI2010] Internal White Points

Description

On an infinite square grid, there are $n$ black vertices, and all other vertices are white (grid vertices are points with integer coordinates, also called lattice points). Every second, all internal white points turn black simultaneously, until there are no internal white points left. Your task is to count the number of black points in the grid at the end. Definition of an internal white point: a white lattice point $P(x,y)$ is an internal white point if and only if there is at least one black point to the left and to the right of $P$ on the same horizontal line (i.e., there exist $x_1 < x < x_2$ such that $(x_1,y)$ and $(x_2,y)$ are both black), and at least one black point above and below $P$ on the same vertical line (i.e., there exist $y_1 < y < y_2$ such that $(x,y_1)$ and $(x,y_2)$ are both black).

Input Format

The first line contains an integer $n$, the number of initial black points. The next $n$ lines each contain two integers $x$, $y$, the coordinates of a black point. No two black points have the same coordinates, and the absolute value of each coordinate does not exceed $10^9$.

Output Format

Output one line containing the final number of black points. If the recoloring process never terminates, output `-1`.

Explanation/Hint

**Constraints** For $36\%$ of the testdata, $n \le 500$. For $64\%$ of the testdata, $n \le 3 \times 10^4$. For $100\%$ of the testdata, $n \le 10^5$. Translated by ChatGPT 5