Pyramid Base

Find the side length of the largest axis-aligned square on a grid that avoids all given rectangular obstacles.

Hard8Binary searchGeometrySegment treeSortingNo attempts yetTime limit5sMemory limit128 MB

Problem

You are looking for the largest site where the base of a new pyramid can stand. A survey has divided the available land into a grid of MM columns and NN rows of square cells. The base of the pyramid is a square, and its sides must be parallel to the sides of the grid.

The survey found PP obstacles, which may overlap each other. Every obstacle is a rectangle whose sides are parallel to the sides of the grid. To build the pyramid, no obstacle may remain on any cell that the base covers. Removing obstacle ii costs CiC_i, and an obstacle is always removed as a whole. You cannot remove only part of an obstacle. Removing one obstacle leaves every obstacle that overlaps it in place.

You are given the grid size MM and NN, the position and the removal cost of each of the PP obstacles, and the budget BB. Write a program that finds the largest side length of a base you can prepare while the total removal cost stays within BB.

Input

The first line contains MM and NN, separated by a single space. (1M,N10000001 \le M, N \le 1\,000\,000)

The second line contains the budget BB. In this problem BB is always 00.

The third line contains PP, the number of obstacles. (1P4000001 \le P \le 400\,000)

Each of the next PP lines describes one obstacle. The iith of those lines contains five integers Xi1X_{i1}, Yi1Y_{i1}, Xi2X_{i2}, Yi2Y_{i2}, CiC_i, separated by single spaces. The first four are the coordinates of the bottommost leftmost cell and of the topmost rightmost cell of obstacle ii, and CiC_i is the cost of removing that obstacle. The bottommost leftmost cell of the grid has coordinates (1,1)(1, 1), and the topmost rightmost cell has coordinates (M,N)(M, N). (1Xi1Xi2M1 \le X_{i1} \le X_{i2} \le M, 1Yi1Yi2N1 \le Y_{i1} \le Y_{i2} \le N, 1Ci70001 \le C_i \le 7\,000)

Output

On the first line, print the largest side length of a pyramid base you can prepare. If no pyramid can be built at all, print 00.

Note

The figure shows the layout of the example. The square drawn there is the only place where a base of side length 33 fits.