Under construction

Still being built. Things may change

First Last

Time limit1sMemory limit2048 MB

Summary
Treat each word as an edge from its first letter to its last letter, then count the words that let Alice force a win when she picks them first in the chain game.
Level

Medium6 of 10

Topics
Game theory, Graph
Solved
No attempts yet

Problem

Alice and Bob are playing a word game. They start with a list of words and take turns. Alice goes first and chooses a starting word from the list. On each later turn, the current player must choose a word from the list that starts with the letter that the previous player's word ends with. No word can be used more than once. A player who cannot choose a word loses.

Assuming both play optimally, how many words in the list, when Alice chooses one as her first word, lead to a win for her?

Input

The first line contains a single integer nn (1≤n≤1 0001 \le n \le 1\,000), the number of words. Each of the next nn lines contains one word made only of the lowercase letters 'a' through 'z'. Each word is two to fifteen letters long. All words are distinct. At most three distinct letters appear at the beginnings and ends of all words. Alice and Bob may only choose words from this list.

Output

Output a single integer: the number of words that force a win for Alice if she chooses them first, assuming both players play optimally.

Examples2

  1. Example 1

    Input
    3
    attic
    climb
    alpha
    
    Expected output
    2
    
  2. Example 2

    Input
    22
    agora
    alpha
    antic
    aorta
    apnea
    arena
    aroma
    attic
    basic
    blurb
    china
    circa
    civic
    climb
    cobra
    cocoa
    comic
    comma
    conic
    crumb
    cubic
    cynic
    
    Expected output
    6