Rhyme

Given N distinct words, find the longest sequence using each word at most once where consecutive words rhyme: their longest common suffix has length at least the longer word's length minus one.

Hard8StringTrieGraphDFSNo attempts yetTime limit1sMemory limit256 MB

Problem

Adrian likes rhymes. He decides that two words rhyme if and only if the length of their longest common suffix equals the length of the longer word, or falls exactly 1 short of it. In other words, words AA and BB rhyme if and only if

LCS(A,B)max(A,B)1\mathrm{LCS}(A, B) \ge \max(|A|, |B|) - 1

Here LCS(A,B)\mathrm{LCS}(A, B) is the length of the longest common suffix of AA and BB, and A|A| is the length of AA.

One day, while reading a collection of short stories, Adrian decided to lay out the longest possible sequence of words in which every two consecutive words rhyme. The words come from the given list, and no word may be used twice.

Adrian got tired of the task and went back to reading. Given NN words, write a program that computes the maximum length of such a sequence.

Input

The first line contains the number of words NN (1N5000001 \le N \le 500\,000).

Each of the next NN lines contains one word. Every word consists of lowercase English letters, and all words are distinct. The sum of the word lengths is at most 30000003\,000\,000.

Output

Print the length of the longest sequence.