Problem H
Line Ball
The Pokemon Center has just come out with a brand new type of Poké Ball. This Poké Ball, called the Line Ball, is more powerful than any other ball created before it. When thrown, the Line Ball travels forward in a line in the direction you are facing, catching all Pokemon it encounters with a $100\% $ success rate!
The Pokemon world is a large $m \times m$ $2$D grid, where the bottom-left square of the grid has coordinate $(0,0)$ and the top-right square of the grid has coordinate $(m-1, m-1)$. In the world, there are $n$ Pokemon, each of which is located on a square of the grid. Before throwing each Line Ball, you are allowed to move freely to any square of the Pokemon world (including a square that a Pokemon is already on) and face one of the four cardinal directions. The Line Ball you throw will then travel forward in the direction you are facing, capturing all Pokemon it passes (including any Pokemon on the same square as you).
You have been given a map that describes the location of every Pokemon, and wish to capture all the Pokemon with Line Balls without spending extra money. Calculate the minimum number of Line Balls you must throw in order to capture every Pokemon.
Input
The first line will contain two space-separated integers, $n$ and $m$ ($1 \leq n,m \leq 10\, 000$), representing the number of Pokemon and the size of the Pokemon world.
$n$ lines will follow, each containing $2$ space-separated integers, $x_ i$ and $y_ i$ ($0 \leq x_ i, y_ i < m$), which are the coordinates of each Pokemon. It is possible for multiple Pokemon to be on the same square of the grid.
Output
Output one line containing the minimum number of Line Balls that you would need to purchase to capture all Pokemon.
Sample Input 1 | Sample Output 1 |
---|---|
3 5 1 2 2 1 2 2 |
2 |
Sample Input 2 | Sample Output 2 |
---|---|
5 10 3 7 2 7 9 4 9 7 3 5 |
3 |