The ancient Berlanders believed that the longer the name, the more important its bearer is. Thus, Berland kings were famous for their long names. But long names are somewhat inconvenient, so the Berlanders started to abbreviate the names of their kings. They called every king by the first letters of its name. Thus, the king, whose name was Victorious Vasily Pupkin, was always called by the berlanders VVP.

In Berland over its long history many dynasties of kings replaced each other, but they were all united by common traditions. Thus, according to one Berland traditions, to maintain stability in the country, the first name of the heir should be the same as the last name his predecessor (hence, the first letter of the abbreviated name of the heir coincides with the last letter of the abbreviated name of the predecessor). Berlanders appreciate stability, so this tradition has never been broken. Also Berlanders like perfection, so another tradition requires that the first name of the first king in the dynasty coincides with the last name of the last king in this dynasty (hence, the first letter of the abbreviated name of the first king coincides with the last letter of the abbreviated name of the last king). This tradition, of course, has also been always observed.

The name of a dynasty is formed by very simple rules: we take all the short names of the kings in the order in which they ruled, and write them in one line. Thus, a dynasty of kings "ab" and "ba" is called "abba", and the dynasty, which had only the king "abca", is called "abca".

Vasya, a historian, has recently found a list of abbreviated names of all Berland kings and their relatives. Help Vasya to find the maximally long name of the dynasty that could have existed in Berland.

Note that in his list all the names are ordered by the time, that is, if name A is earlier in the list than B, then if A and B were kings, then king A ruled before king B.

Input

The first line contains integer n (1 ≤ n ≤ 5·105) — the number of names in Vasya's list. Next n lines contain n abbreviated names, one per line. An abbreviated name is a non-empty sequence of lowercase Latin letters. Its length does not exceed 10 characters.

Output

Print a single number — length of the sought dynasty's name in letters.

If Vasya's list is wrong and no dynasty can be found there, print a single number 0.

Examples

Input

3
abc
ca
cba


Output

6


Input

4
vvp
vvp
dam
vvp


Output

0


Input

3
ab
c
def


Output

1

Note

In the first sample two dynasties can exist: the one called "abcca" (with the first and second kings) and the one called "abccba" (with the first and third kings). 

In the second sample there aren't acceptable dynasties.

The only dynasty in the third sample consists of one king, his name is "c".
import java.io.*;
import java.util.*;

public class CodeForces {
    /*****************************************WRITING CODE STARTS HERE******************************************/
/*
5
cab
bbc
caa
ccc
aca
 */

    public static void solve(int t)throws IOException {
        int n = sc.nextInt();
        int[][] dp = new int[26][26];
        while(n-->0){
            String str = sc.next();
            int start = str.charAt(0) - 'a';
            int end = str.charAt(str.length()-1) - 'a';
            if(start!=end)dp[start][end] = max2(dp[start][end],str.length());
            for(int i=0;i<26;i++){
                if(i==start && i == end){
                    dp[i][i] +=str.length();
                    continue;
                }
                if(dp[i][start]>0) {
                    // [i,start] string is present
                    dp[i][end] = max2(dp[i][end], dp[i][start] + str.length());
                }

            }
        }
        int ans = 0;
        for(int i=0;i<26;i++)
            ans = max2(ans,dp[i][i]);
        out.println(ans);
    }


    /*****************************************WRITING CODE ENDS HERE******************************************/

    public static void main(String[] args) throws IOException{
        int testCase = 1;
//        testCase = sc.nextInt();
        for (int i = 1; i <= testCase; i++) solve(i);
        closeIO();
    }
    static FastestReader sc = new FastestReader();              // FASTEST INPUT
    static PrintWriter out = new PrintWriter(System.out);                // FAST OUTPUT


    /**************************************HELPER FUNCTION STARTS HERE ************************************/

    public static int mod = (int) 1e9 + 7;
    public static final int inf_int = (int) 1e9;
    public static final long inf_long = (long) 1e15;

    public static final String YES = "YES";
    public static final String NO = "NO";

    // euclidean algorithm time O(max (loga ,logb))
    public static long gcd(long a, long b) {
        long x = Math.min(a, b);
        long y = Math.max(a, b);
        if (y % x == 0) return x;
        return gcd(y % x, x);
    }

    public static long lcm(long a, long b) {
        // lcm(a,b) * gcd(a,b) = a * b
        return (a / gcd(a, b)) * b;
    }
    // binary exponentiation time O(logn)
    public static long power(long x, long n) {
        long ans = 1;
        while (n > 0) {
            if ((n & 1) == 1) {
                ans *= x;
                ans %= mod;
                n--;
            } else {
                x *= x;
                x %= mod;
                n >>= 1;
            }
        }
        return ans;
    }

    public static long firstDivisor(long n) {
        if (n == 1 || n == 0) return n;
        for (long i = 2; i * i <= n; i++)
            if (n % i == 0) return i;
        return -1;
    }

    public static long max3(long a, long b, long c) {
        return max2(max2(a, b), c);
    }

    public static long max2(long a, long b) {
        return Math.max(a, b);
    }

    public static long min3(long a, long b, long c) {
        return min2(min2(a, b), c);
    }

    public static long min2(long a, long b) {
        return Math.min(a, b);
    }

    public static int max3(int a, int b, int c) {
        return max2(max2(a, b), c);
    }

    public static int max2(int a, int b) {
        return Math.max(a, b);
    }

    public static int min3(int a, int b, int c) {
        return min2(min2(a, b), c);
    }

    public static int min2(int a, int b) {
        return Math.min(a, b);
    }

    /**************************************HELPER FUNCTION ENDS HERE ************************************/

    /*****************************************FAST INPUT STARTS HERE *************************************/


    public static void closeIO() throws IOException{
        out.flush();
        out.close();
        sc.close();
    }
    private static class FastestReader {
        private static final int BUFFER_SIZE = 1 << 16;
        private final DataInputStream din;
        private final byte[] buffer;
        private int bufferPointer, bytesRead;

        public FastestReader() {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public FastestReader(String file_name) throws IOException {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }

        private int skip() throws IOException {
            int b;
            //noinspection StatementWithEmptyBody
            while ((b = read()) != -1 && isSpaceChar(b)) {}
            return b;
        }

        public String next() throws IOException {
            int b = skip();
            final StringBuilder sb = new StringBuilder();
            while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ')
                sb.appendCodePoint(b);
                b = read();
            }
            return sb.toString();
        }

        public int nextInt() throws IOException {
            int ret = 0;
            byte c = read();
            while (c <= ' ') {
                c = read();
            }
            final boolean neg = c == '-';
            if (neg) { c = read(); }
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');

            if (neg) { return -ret; }
            return ret;
        }

        public long nextLong() throws IOException {
            long ret = 0;
            byte c = read();
            while (c <= ' ') { c = read(); }
            final boolean neg = c == '-';
            if (neg) { c = read(); }
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            if (neg) { return -ret; }
            return ret;
        }

        public double nextDouble() throws IOException {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ') { c = read(); }
            final boolean neg = c == '-';
            if (neg) { c = read(); }

            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');

            if (c == '.') {
                while ((c = read()) >= '0' && c <= '9') {
                    ret += (c - '0') / (div *= 10);
                }
            }

            if (neg) { return -ret; }
            return ret;
        }

        private void fillBuffer() throws IOException {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1) { buffer[0] = -1; }
        }

        private byte read() throws IOException {
            if (bufferPointer == bytesRead) { fillBuffer(); }
            return buffer[bufferPointer++];
        }

        public void close() throws IOException {
            din.close();
        }

    }
    /*****************************************FAST INPUT ENDS HERE *************************************/
}