House Rental

Time limit2sMemory limit512 MB

Summary
Given facilities of k types on a line, find the integer location minimizing the largest distance to the nearest facility of each type, smallest such location on ties.
Level

Medium7 of 10

Topics
Binary search, Sorting, Two pointers
Solved
No attempts yet

Problem

Acmi is a medium-sized city with one long main street running from west to east. Every facility in Acmi, such as a supermarket, a school, a train station, a bus stop, or a hospital, sits on that main street. You are going to rent a house on the main street, and location is what you care about most when you choose one. You have a list of kk facility types, and you want the distance to each of those types to be as small as possible. If a supermarket matters to you and is on your list, you want the house to be near a supermarket.

When k=1k = 1 the problem is easy, because you rent the house right in front of any facility of the type you want. In the general case with k>1k > 1 that strategy stops working.

Given nn facilities of kk different types along the main street, write a program that finds a best location for your house, one that minimizes the largest of the distances to the nearest facility of each type. The location of every building on the main street is an integer from −1000000000-1000000000 to 10000000001000000000, and the smaller the location number, the further west the building is. The distance between two buildings is the difference of their location numbers. Two or more facilities may share one location, and each of the kk types has at least one facility. A vacant house you can rent is available at every integer location.

Input

The first line contains two integers kk and nn (1≤k≤1000001 \le k \le 100000, k≤n≤1000000k \le n \le 1000000), where kk is the number of facility types and nn is the number of facilities along the main street.

Each of the next nn lines describes one facility with two integers: its location on the street, between −1000000000-1000000000 and 10000000001000000000 inclusive, and its type, between 11 and kk.

Output

Print one line with one integer, a best location for your house. If two or more locations are best, print the smallest location number among them.

Examples3

  1. Example 1

    Input
    1 5
    -100 1
    -10 1
    0 1
    1 1
    2 1
    
    Expected output
    -100
    
  2. Example 2

    Input
    5 5
    -2 1
    0 3
    -1 2
    1 4
    2 5
    
    Expected output
    0
    
  3. Example 3

    Input
    3 6
    0 1
    6 2
    7 3
    0 2
    1 3
    5 1
    
    Expected output
    0