Fako Against the World

View as PDF

Submit solution

Points: 1
Time limit: 1.0s
Memory limit: 256M

Problem types

Fako has returned and once again wants to destroy our world. Help us destroy the temples Fako has built all around the world.

Fako has \(N\) temples and they encrypted the entrances of each temple with a puzzle of magical words. If you can arrange the words in such a way that the first letter of each magical word is equal to the last letter of the previous magical word, we will be able to enter the temple and destroy the power circuits inside. Can you help us find the number of temples that can be destroyed?

Input

The first line will contain the integer \(N\), the total number of temples.

For each temple, there will be an integer \(M_i\), denoting the number of words in the \(i\)th temple's puzzle. The following \(M_i\) lines will give the words. The magical words contain only lowercase letters.

  • \(1 \leq N \leq 400\)
  • \(2 \leq M_i \leq 10^5\)
  • \(1 \leq\) Word Length \(\leq 20\)

Output

In a single line, you should print the number of temples that we can enter (and destroy!).

Examples

Input 1:

3
2
jan
na
3
sevdan
zarar
oldu
3
ee
be
en

Output 1:

2

Input 2:

1
4
f
p
g
a

Output 2:

0

Explanations

1st Input
  • In the first temple jan \(\rightarrow\) na is a valid way to destroy the temple.
  • In the second temple, there is no valid combination to destroy the temple.
  • In the last temple be\(\rightarrow\) ee \(\rightarrow\) en is a valid way to destroy the temple.

Therefore 2 temples can be destroyed in total.