#P9752. Five-Dial Lock Security

    ID: 22898 Type: Default 1000ms 256MiB

Five-Dial Lock Security

Five-Dial Lock Security

Little Y uses a five‐dial combination lock to secure his bicycle. Each dial is labeled with the digits from 00 to 99 in a cyclic order (i.e. after 99 comes 00, and before 00 comes 99). When locking his bicycle, starting from the correct password, he performs a single random move. In each move, he rotates either a single dial or two adjacent dials by the same nonzero amount dd (where d{1,2,,9}d\in\{1,2,\ldots,9\}) in one direction. For example, he can change the lock state from 0 0 1 1 5 to 1 1 1 1 5 by rotating the first two dials by +1+1, but he cannot change it to 1 2 1 1 5 because the two dials must be rotated by the same amount.

After some time, Little Y worries about the security of his locking method. He has recorded nn lock states after locking his bicycle (note that none of these states is the correct password).

Your task is to count the number of possible correct passwords PP (each a 5‐digit number, with possible leading zeros) such that for every recorded state SS, there exists a valid move (rotating either a single dial or two adjacent dials by a nonzero amount) which transforms PP into SS. In other words, when comparing P=p0p1p2p3p4P = p_0p_1p_2p_3p_4 and a recorded state S=s0s1s2s3s4S=s_0s_1s_2s_3s_4, the differences computed modulo 1010 must be all zero except for either exactly one position or exactly two consecutive positions where the difference is the same nonzero value.

inputFormat

The first line contains an integer nn (1n1001\le n\le 100), the number of recorded states. Each of the next nn lines contains a string of 5 digits representing a recorded state of the lock (possibly with leading zeros).

outputFormat

Output a single integer: the number of possible correct passwords that can generate every recorded state via a single valid move.

sample

1
00115
81