Cash Dispenser

No attempts yetTime limit3sMemory limit128 MB

Problem

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.

Input

The first line contains an integer nn, the number of recorded scenes of the client entering the PIN (1n10001 \le n \le 1000).

Each of the next nn lines describes one scene as two values separated by a single space: an integer tt, the length of the movement sequence (1t100001 \le t \le 10000), followed by a tt-digit number whose consecutive digits are the keys the finger passed over, in order.

The total length of all sequences does not exceed 10000001\,000\,000.

Output

Print a single integer: the number of 4-digit PIN codes that are consistent with all of the recorded movement sequences.