Daunting device

Apply N range-recolor operations whose endpoints depend on the current count of a query color, then report the highest cell frequency.

Hard8Segment treeImplementationSortingMathNo attempts yetTime limit1sMemory limit1024 MB

Problem

At an excavation site on the Caribbean island of Saint Basil you found an old device together with a set of instructions that reads like a puzzle. Your local guide Vibenas says that solving the puzzle makes the device point to the place where the pirate Lyerpes hid a treasure.

The device holds a tape of LL cells indexed from 00 to L1L-1. Each cell carries one color, and a command sent to the device changes that color. Colors are written as integers, and every cell starts with the same color.

The instructions list NN steps to perform before the device shows the way. Each step is given by four integers PP, XX, AA and BB. To perform a step, first count the cells whose current color is PP. Call that count SS, then compute the two values

M1=(A+S2)modLM_1 = (A + S^2) \bmod L

M2=(A+(S+B)2)modLM_2 = (A + (S + B)^2) \bmod L

Finally, change every cell in the closed interval [min(M1,M2),max(M1,M2)][\min(M_1, M_2), \max(M_1, M_2)] to color XX.

After all NN steps, pick a color that appears the greatest number of times on the tape and report how many cells carry it. That number is the same even when several colors tie for the greatest count.

Input

The first line contains three integers LL, CC and NN (1L,C,N1051 \le L, C, N \le 10^5), which are the number of cells on the tape, the number of available colors, and the number of steps. Colors are the distinct integers from 11 to CC, and every cell starts with color 11.

Each of the next NN lines describes one step with four integers PP, XX, AA and BB (1P,XC1 \le P, X \le C, 0A,B1080 \le A, B \le 10^8). PP is the color whose cell count decides the range of the step, XX is the color the cells in the range must have after the step is performed, and AA and BB are used to compute the bounds of the range as described above. The steps are performed in the given order.

Output

Print one line with the number of cells that have a color appearing the greatest number of times on the tape after all steps have been performed.