#P12140. LQ Mall Lucky Draw

    ID: 14240 Type: Default 1000ms 256MiB

LQ Mall Lucky Draw

LQ Mall Lucky Draw

LQ Mall is offering a lucky draw to thank its customers. The draw works with a slot machine that has three rotating wheels. Each wheel displays n digit patterns, consecutively numbered from 1 to n in a cyclic order. Initially, all three wheels point at the pattern 1. Every draw, each wheel rotates forward a specified number of positions (wrapping around after n) determined by random integers.

The prizes are awarded based on the outcome shown by the three wheels:

  1. If all three patterns are identical, you win 200 points.
  2. If exactly two patterns are identical, you win 100 points.
  3. If the three patterns form a strictly increasing consecutive sequence from left to right (i.e. $a, a+1, a+2$), you win 200 points.
  4. If the three patterns, after rearrangement, can form a consecutive sequence (i.e. they are distinct and the maximum minus the minimum is 2), you win 100 points.

Note: For each draw, at most one prize may be awarded. If more than one condition is met, the highest point prize is given.

Given m draws, compute the total points accumulated after all draws.

Details on the rotation:

Let the initial positions of the three wheels be p1 = p2 = p3 = 1. For each draw i, you are given three integers xi1, xi2, xi3. The new position of the j-th wheel is computed as:

\[ p_j = ((p_j - 1 + x_{ij}) \mod n) + 1 \]

inputFormat

The first line contains two integers n and m separated by a space, where n is the number of digit patterns on each wheel and m is the number of draws.

Each of the next m lines contains three integers xi1, xi2, xi3 separated by spaces, representing the number of positions each wheel rotates in the i-th draw.

outputFormat

Output a single integer, the total points accumulated after all the draws.

sample

5 3
1 1 1
1 1 1
2 2 2
600

</p>