A bank's clients withdraw money from cash dispensers using a card and a 4-digit PIN. To protect clients from thieves who intercept the camera footage, a client makes extra, redundant finger movements over the keypad while entering the PIN. The camera cannot tell which keys were actually pressed; it records only the sequence of keys the finger passes over. As a result, the PIN usually cannot be determined uniquely.
For example, if a client moves his finger over key 1 and then over key 5, he could have entered any of these PINs: 1111, 1115, 1155, 1555, 5555. In other words, a PIN is consistent with a recorded movement when its four digits can be read off the recorded key sequence in order: each keypress happens over one of the keys the finger passes over, and the keypresses follow the direction of movement (a key may be pressed several times in a row while the finger rests on it).
You are given several recordings of the finger movements the same client made while entering the same PIN. Determine how many distinct 4-digit PINs are consistent with every recording, i.e. how many 4-digit codes could have produced all of the recorded movement sequences.
The first line contains an integer n, the number of recorded scenes of the client entering the PIN (1≤n≤1000).
Each of the next n lines describes one scene as two values separated by a single space: an integer t, the length of the movement sequence (1≤t≤10000), followed by a t-digit number whose consecutive digits are the keys the finger passed over, in order.
The total length of all sequences does not exceed 1000000.
Print a single integer: the number of 4-digit PIN codes that are consistent with all of the recorded movement sequences.