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

#include <iostream>
#include <vector>
#include <limits>
#include <queue>
#include <cstdlib>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <map>
#include <math.h>
#include <algorithm>
#include <limits>
#include <iterator>
#include <ctype.h>
#include <complex>
using namespace std;
typedef long long ll;
int n,nx;
char s[500001][15];
int l[500001],maxl[500001][30],sol=0;
/*vector<int>ady[500001];
void dfs(int nodo,int len){
    int x;
    if(s[nodo][l[nodo]-1]==s[nx][0])
        sol=max(sol,len);
    x=ady[nodo].size();
    for(int i=0;i<x;i++)
        dfs(ady[nodo][i],len+l[ady[nodo][i]]);
}*/
vector<int>end[30];
int main() {
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%s",s[i]);
        l[i]=strlen(s[i]);
        maxl[i][s[i][0]-'a']=l[i];
        for(int j=end[s[i][0]-'a'].size()-1;j>=0;j--){
            nx=end[s[i][0]-'a'][j];
            for(int k='a';k<='z';k++)
                maxl[i][k-'a']=max(maxl[i][k-'a'],maxl[nx][k-'a']+l[i]);
            //ady[].push_back(i);
        }
        sol=max(sol,maxl[i][s[i][l[i]-1]-'a']);
        end[s[i][l[i]-1]-'a'].push_back(i);
    }
    /*for(int i=0;i<n;i++){
        nx=i;
        dfs(i,l[i]);
    }*/
    printf("%d\n",sol);
    return 0;
}
