Pyramid Base 2

Find the largest axis-aligned square that fits on the grid so the total cost of removing the obstacles it overlaps stays within budget.

Medium7Binary searchSegment treeGeometryNo attempts yetTime limit5sMemory limit128 MB

Problem

You need the largest pyramid site your budget allows. A land survey divides the site into an M×NM \times N grid of square cells. The base of the pyramid must be a square with sides parallel to the grid.

The survey found PP obstacles. Each obstacle is a rectangle with sides parallel to the grid, and obstacles may overlap. To build the pyramid, every cell covered by its base must be cleared of obstacles. Removing obstacle ii costs CiC_i. An obstacle can only be removed whole, never in part. Removing one obstacle leaves any obstacle that overlaps it untouched.

Given the dimensions MM and NN, the position and removal cost of each of the PP obstacles, and the budget BB, write a program that finds the largest possible side length of the base such that the total removal cost does not exceed BB.

Input

The first line contains MM and NN, separated by one space. (1M,N1061 \le M, N \le 10^6)

The second line contains the budget BB. (0<B2×1090 < B \le 2 \times 10^9)

The third line contains PP, the number of obstacles. (1P300001 \le P \le 30000)

Each of the next PP lines describes one obstacle. The iith of these lines describes obstacle ii and contains five integers Xi1X_{i1}, Yi1Y_{i1}, Xi2X_{i2}, Yi2Y_{i2}, CiC_i, separated by single spaces. The first two are the coordinates of the bottommost leftmost cell of the obstacle, the next two are the coordinates of the topmost rightmost cell, and the last 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 7000)

Output

Print one integer, the largest side length of the base that can be prepared. If no pyramid can be built at all, print 00.

Note

The figure shows two placements of a base with side length 44.