The Sprawl
Time limit5sMemory limit768 MB
On an infinite grid, N cities grow one cell per day in index order; sum over all pairs the first day the two cities' territories touch and connect.
Problem
Kim is playing a city-building simulation game on an infinite chessboard. Every cell is denoted by two integers . Two cells are adjacent if they share an edge. Formally, cell is adjacent to four cells, .
In the beginning (day 0), city occupies the single cell . If a cell is occupied by any city, it is called a developed cell. Thus, on day 0, there are exactly developed cells.
After each day, each city grows and expands by occupying any undeveloped adjacent cells. Formally, for each city , let be the set of cells adjacent to at least one cell of city . Starting from city 1 to city , any undeveloped cell in becomes part of city and becomes developed.
Two cities are "connected" when you can move from to passing only between adjacent developed cells. For two cities , let be the first day on which cities become connected. Kim is interested in each value of , but there are too many values to consider. Thus, for the given starting cells, Kim only wants to calculate .
Input
The first line contains the number of cities .
The next lines give the starting cell of the -th city as two integers .
Output
Print , where is the first day on which cities become connected.
Constraints
- All given cells are distinct