Mushroom tractor
InterviewTime limit2sMemory limit32 MB
Mushrooms appear one per second, and the goal is the earliest second when some row, column, or diagonal contains at least K of them.
Problem
Mirko got a tractor for Christmas that can pick mushrooms. The mushrooms grow on a square meadow. In the coordinate plane that meadow is the square with lower left corner and upper right corner .
At the start there is no mushroom on the meadow. Every second exactly one new mushroom grows on an empty spot, and of them grow in total. The -th mushroom grows at second .
Mirko wants to drive the tractor once and pick at least mushrooms. He starts at one of the lattice points of the meadow, and he moves only in a direction parallel to a side of the meadow or parallel to one of its diagonals. The tractor is so fast that the ride takes no time, and that speed also keeps him from turning during the ride. One ride therefore picks every mushroom on the line fixed by his starting point and his direction.
Find the smallest number of seconds after which Mirko can pick as many mushrooms as he wants.
Input
The first line contains the number of mushrooms that will grow and the number of mushrooms Mirko wants to pick . (, )
Each of the next lines contains the coordinates and of the -th mushroom to grow. () A mushroom grows only on an empty spot, so no two mushrooms have the same coordinates.
Output
Print the smallest required number of seconds. If Mirko cannot pick mushrooms in one ride, print -1.
Hint
In the first example Mirko starts at and rides toward the mushroom at .