cho.sh
Notes
Loading...

Wire Cutting

Time limit

1s

Memory limit

128 MB

Problem

There is a grid made of N horizontal lines and N vertical lines. The distance between two adjacent horizontal lines or two adjacent vertical lines is 1. An intersection is written as (x, y) when it lies on the x-th vertical line from the left and the y-th horizontal line from the bottom. The bottom-left intersection is S = (1, 1).

A single wire is placed on the grid under these rules:

  1. The wire lies only on horizontal and vertical grid lines.
  2. The wire may turn only at grid intersections.
  3. The wire may overlap itself.
  4. Both endpoints of the wire are S, and the endpoints are joined together.

The input gives the bend points of the wire in the order they are visited. The wire starts at S, visits those bend points in order, and then returns to S.

The grid is then cut by a vertical line parallel to the grid's vertical lines. This cutting line passes exactly halfway between the I-th vertical line and the (I + 1)-th vertical line. It is guaranteed that the cut divides the wire into at least two pieces.

Given the wire and the cutting line, find the length of the longest piece of wire after the cut.

Input

The first line contains N, the number of horizontal lines and also the number of vertical lines (2 <= N <= 1,000,000).

The second line contains K, the number of bend points of the wire (1 <= K <= 100,000).

Each of the next K lines contains two integers x and y, the coordinates of a bend point.

The last line contains I, meaning that the cutting line passes between the I-th vertical line and the (I + 1)-th vertical line (1 <= I <= N - 1).

Output

Print the length of the longest piece among the pieces created by the cut.