10
abdcced
bdacdac
ecb
dc
eaeeebdd
adcdbadcac
eb
aadecccde
edbaeacad
accd
 100
d
a
e
eb
c
d
d
e
c
e
ac
d
c
aa
a
e
da
e
e
c
e
a
c
c
bc
bd
d
da
cc
be
b
a
ee
d
be
be
e
b
a
ed
cb
ab
a
ea
ac
aa
ed
d
b
eb
e
ed
c
ce
c
d
c
d
d
ee
e
b
ea
ea
da
ab
ba
ed
da
ec
ea
bd
ee
dc
d
bd
d
c
cc
ab
bd
a
ad
e
d
e
bc
bb
c
bc
a
a
e
ca
ac
c
ad
c
be
d
 3
ab
acb
ba
 10
a
b
a
c
b
a
b
a
a
a
 5
cab
bbc
caa
ccc
aca
 3
ac
abc
ca
 5
ab
bc
cd
de
ffffffffff
 16
ddgcb
fgbhgbdg
gha
dh
ddfb
fggbc
edb
hd
d
hfgcabf
hdaef
fhbcdgba
chee
cafeggc
f
ecb
 50
agecd
cghafi
fiide
iecc
jbdcfjhgd
diee
hfeg
ehc
gfijgjh
gacaifebg
dicbbddc
hjgciaei
jjcdh
g
gc
cf
hfdjhd
c
icidbec
ji
eeh
cgeejggc
acfd
jjg
ajefdj
eghhebiic
bih
gbb
jjaa
idc
gafi
ecg
dbigbjiehj
cedif
ahidfaaajg
hfhhiccbgb
dgegjgieif
hgjebhfdc
j
ahehd
ahdah
bijbhh
a
dabiggfhac
dedigbjbf
ihbdj
iihagchd
gfebhfdi
jbjdcggdh
f

0
 35
 5
 6
 9
 5
 10
 7
 37


import java.util.Scanner;

public class DynastyPuzzles {
    static int n;
    static int[] start,end,length;
    static int[][] arr=new int[26][26];

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        int[][] path = new int[26][26];
        start = new int[n+1];
        end = new int[n+1];
        length = new int[n+1];
        for( int i=0; i<n; i++) {
            char[] name = sc.next().toCharArray();
            length[i] = name.length;
            start[i] = name[0]-'a';
            end[i] = name[length[i]-1]-'a';
        }
        for(int i=0; i<n; i++) {
            int s = start[i];
            int e = end[i];
            for( int k=0; k<26;k++) {
                if(path[k][s] != 0) {
                    path[k][e] = Math.max(path[k][e],path[k][s]+length[i]);
                }
            }
            path[s][e] = Math.max(path[s][e],length[i]);
        }
        int ans = 0;
        for( int i=0;i<26;i++) {
            ans = Math.max(ans,path[i][i]);
        }
        System.out.println(ans);
    }
    public static void solve(int index, int prevEnd) {
        if(index == n) return ;
        if(start[index] == prevEnd) {
            for(int i=0; i<26; i++) {
                if( arr[i][start[index]] != 0)
                arr[i][end[index]] = Math.max(arr[i][end[index]],arr[i][start[index]]+length[index]);
            }
            arr[start[index]][end[index]] = Math.max(arr[start[index]][end[index]],length[index]);
            solve(index+1,end[index]);
        }
        arr[start[index]][end[index]] = Math.max(arr[start[index]][end[index]],length[index]);
        solve(index+1,end[index]);
    }
    /*public static int solve(int i, int prevStart,int prevEnd,int[][] arr) {
        if( i == n ) return 0;
        int ans = 0;
        arr[start[i]][end[i]] = length[i];
        if(start[i] == prevEnd) {
            arr[prevStart][end[i]] = Math.max(arr[prevStart][end[i]],arr[prevStart][end[i]]+length[i]);
            ans = Math.max(ans,solve(i+1,prevStart,end[i],arr)+arr[prevStart][end[i]]);
        }

        ans = Math.max(ans,solve(i+1,start[i],end[i],arr)+length[i]);

        return ans;
        *//*if( start[i] == prevEnd ) {
            ans = Math.max(solve(i+1,prevStart,prevEnd,prevLen),solve(i+1,start[i],end[i],length[i])+prevLen);
        }
        else if(start[i] == end[i]) {
            ans = Math.max(ans,solve(i+1,start[i],end[i],length[i]));
        }
        ans = Math.max(ans,solve(i+1,start[i],end[i],));*//*
    }*/
}
