In a recent trip to an excavation site in the Caribbean island of Saint Basil, you found a mysterious device with some instructions resembling a puzzle. Your local guide Vibenas tells you that if you solve the puzzle, the device might show you the place where a big treasure left by the old merciless pirate Lyerpes is hidden.
The device has a tape with $L$ cells indexed from $0$ to $L - 1$. Each cell has a color than can be changed with commands to the device. Each color is encoded by an integer, and initially all cells have the same color. The instructions that you found represent $N$ steps to be performed before the device shows the way to the treasure. Each step is described using four integers $P$, $X$, $A$ and $B$. The instructions say that to complete the step you must first count the number of cells currently having color $P$. Let this number be $S$. Then you must calculate the values
$M_1 = (A + S^2) \texttt{ mod } L$,
$M_2 = (A + (S + B)^2) \texttt{ mod } L$,
Finally you have to make all cells within the closed interval $[min(M_1, M_2 ), max(M_1, M_2 )]$ to be of color $X$.
After the exhausting task of processing the $N$ steps required by the device, you still have one job: given a color that appears the greatest number of times in the device tape after all steps (that is, a most frequent color), you must go to the shipwreck of Lyerpes’ legendary vessel and say aloud the number of cells having that color. Note that this number is unique even if more than one color appears the greatest number of times in the device tape after all steps.
Doing all those calculations on the device will take ages but you, as a renowned programmer, can create a program that quickly indicates the answer for the puzzle. After that, the real hard part of your mission will be to find out where is the shipwreck of Lyerpes’ old vessel.