P23633 [AtCoder] Word Chain Game
NOI
题目描述

Problem Description

You have recently become obsessed with a slightly special word chain game and have purchased a "basic word chain set" containing several English words.

The rules of the word chain game are as follows:

  • The words that can be used in the game are predetermined, and no other words can be used. However, when using English words, you may instead speak the reversed version of the word.
  • Once a word has been used, it cannot be used again (if a reversed word is spoken, the original word also becomes unusable).
  • For the first turn, you can freely choose any word.
  • For subsequent turns, the word spoken must start with the letter that the previous word ended with.
  • If there are no words that meet the conditions when you attempt to speak, the game ends immediately.

Normally, a word chain game is played by multiple people, but you, who prefer solitude, aim to use all the words in one game without any leftovers. However, you realize that it might not be possible to achieve your goal with just the words in the "basic word chain set."

Therefore, you decide to purchase some additional words separately and use them along with the words in the "basic word chain set" to achieve your goal. Please answer the minimum number of additional words you must purchase.

In the world you live in, fortunately, any English word is abundantly available for purchase in any quantity you need.

(Note) Here, an English word refers to a string of lowercase alphabetic characters (a-z) with a length between 2 and 20. Please note that this definition differs from the actual definition of English words.

Input Format

The input is given from the standard input in the following format:

$ N $ $ w_1 $ $ w_2 $ : $ w_N $

  • The first line contains the number of words in the "basic word chain set" $N\ (1\ ≦\ N\ ≦\ 100,000)$.
  • The next $ N $ lines contain the information of the words in the "basic word chain set". The $ i $-th line contains the $ i $-th word $w_i\ (2\ ≦\ |w_i|\ ≦\ 20)$, which consists of lowercase alphabetic characters (a-z).
  • All words are guaranteed to be unique, and no word reversed will match another word.

Output Format

Output the minimum number of additional words you must purchase in one line. Do not forget the newline at the end.

Sample Input and Output

Sample Input #1

3
soup
peas
ecir

Sample Output #1

1

Sample Input #2

4
ba
bc
ca
da

Sample Output #2

0

Sample Input #3

13
ab
cd
ef
gh
ij
kl
mn
op
qr
st
uv
wx
yz

Sample Output #3

12

Sample Input #4

3
aa
xyz
zwx

Sample Output #4

1

Notes/Hints

Sample Explanation 1

With just the words in the "basic word chain set", it is not possible to use them all without any leftovers. By purchasing the word sugar, for example, you can end the game using all words like this: "soup→peas→sugar→rice" (where rice is the reversed version of ecir), achieving your goal.

Sample Explanation 2

As an example, you can play the game like this: "ab→bc→ca→ad", and no additional word purchases are necessary. In this example, ba and da are used in their reversed form.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
3 soup peas ecir
样例输出
1