Hide

Problem E
Tiles

Languages en is

Soon after converting to Christianity, it is believed that the first and the only Lithuanian King Mindaugas ordered the construction of the Vilnius Cathedral. The construction is almost completed, except that the floor has to be covered with ceramic ornamented glazed tiles.

The floor of the Vilnius Cathedral is a polygon in a 2D plane with a Cartesian coordinate system. The polygon has $N$ distinct vertices, numbered from $1$ to $N$. For each $i$ such that $1 \leq i \leq N$, vertex $i$ is located at point $(X[i], Y[i])$, where $X[i]$ and $Y[i]$ are non-negative integers. There is an edge connecting vertex $i$ and vertex $i + 1$ (for each $i$ such that $1 \le i \le N - 1$), as well as an edge connecting vertex $N$ and vertex $1$. The vertices are listed in either clockwise or counterclockwise order.

The cathedral is an axis-aligned polygon, which means that each of the edges is parallel to either the $x$-axis or the $y$-axis. Moreover, the cathedral is a simple polygon, that is:

  • exactly two edges meet at each vertex;

  • any pair of edges can only meet at a vertex.

The builders of the cathedral have infinitely many pieces of tiles. Each piece is a square with side length equal to 2. The builders would like to cover a big part of the cathedral with these pieces. Specifically, the builders want to pick some vertical line and cover the part of the cathedral to the left of the line. For any integer $k$, let $L_ k$ denote the vertical line consisting of points with $x$-coordinate equal to $k$. A covering of the part of the cathedral to the left of $L_ k$ is a placement of some number of pieces in the plane such that:

  • each point which lies in the interior of the polygon and has $x$-coordinate less than $k$ is covered by some piece;

  • no point which lies outside of the polygon or has $x$-coordinate greater than $k$ is covered by some piece;

  • the interiors of the pieces do not overlap.

The minimum $x$-coordinate of any vertex in the cathedral is $0$. Let $M$ denote the maximum $x$-coordinate of any vertex in the cathedral.

Task

Help the builders of the Vilnius Cathedral by determining the largest integer $k$, such that $k \le M$, and there exists a covering of the part of the cathedral to the left of $L_ k$. Note that by definition, there exists a covering of the part of the cathedral to the left of $L_0$ (which uses $0$ pieces).

Input

The first line of the input contains two integers $N$ and $M$ - the number of vertices and the maximum $x$-coordinate of any vertex.

Then, $N$ lines follow. The $i$-th of them contains two integer numbers $x_ i$ and $y_ i$ - the coordinates of $i$-th vertex. The vertices are listed in either clockwise or counterclockwise order.

Output

Your program should output the maximum $k$, such that $k \le M$ and there exists a covering of the part of the cathedral to the left of $L_ k$.

Constraints

  • $4 \leq N \leq 2 \cdot 10^5$

  • $1 \leq M \leq 10^9$

  • $0 \leq y_ i \leq 10^9$ (for each $1 \leq i \leq N$)

  • The cathedral forms an axis-aligned simple polygon.

  • The minimum of $x_1, x_2, \dotsc , x_ N$ is $0$, and the maximum of $x_1, x_2, \dotsc x_ N$ is $M$.

Subtasks

No.

Points

Additional constraints

1

4

$N = 4$.

2

9

$N \le 6$.

3

11

$x_ N = 0, y_ N = 0$, $x\_ i \leq x\_ {i+1}, y\_ i \geq y\_ {i+1}$ (for each $i$ such that $1 \le i \le N-2$).

4

19

$M \le 1000$ and all $y_ i \le 1000$.

5

22

All values of $y_ i$ are even.

6

25

All values of $x_ i$ are even.

7

10

No additional constraints.

Examples

  1. The following picture shows the part of the cathedral to the left of line $L_ k$ for $k = 2$:

    \includegraphics[width=0.5\textwidth ]{tiles-01.png}

    There is a covering of the part of the cathedral to the left of $L_2$. The covering uses two pieces.

    For any $k > 2$, there is no covering of the part of the cathedral to the left of $L_ k$.

  2. There is no positive value of $k$ such that the part of the cathedral to the left of $L_ k$ could be covered with tiles.

  3. As illustrated below, it is possible to cover the part of the cathedral to the left of line $L_6$:

    \includegraphics[width=0.5\textwidth ]{tiles-02.png}

    For each $k > 6$, there is no covering of the part of the cathedral to the left of $L_ k$.

Sample Input 1 Sample Output 1
14 6
0 1
0 3
2 3
2 4
0 4
0 6
3 6
3 7
4 7
6 7
6 5
3 5
3 2
3 1
2
Sample Input 2 Sample Output 2
4 3
0 0
0 3
3 3
3 0
0
Sample Input 3 Sample Output 3
18 9
0 2
2 2
2 1
4 1
4 0
9 0
9 2
4 2
4 4
7 4
7 3
9 3
9 6
4 6
4 5
2 5
2 4
0 4
6

Please log in to submit a solution to this problem

Log in