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 <bits/stdc++.h>
using namespace std;
int n, dp[500005][30], maxx[30][30];
struct cuv {
  int ul, pr;
  int len;
} a[500005];
int main() {
  int i, j, sol = -2000000000, pr;
  char sir[100];
  cin.sync_with_stdio(0);
  cin >> n;
  for (i = 1; i <= n; ++i) {
    cin >> (sir + 1);
    a[i].pr = sir[1] - 'a';
    a[i].len = strlen(sir + 1);
    a[i].ul = sir[a[i].len] - 'a';
  }
  for (i = 1; i <= n; ++i)
    for (j = 0; j < 26; ++j) dp[i][j] = -2000000000;
  for (i = 0; i < 26; ++i)
    for (j = 0; j < 26; ++j) maxx[i][j] = -2000000000;
  dp[1][a[1].ul] = a[1].len;
  maxx[a[1].pr][a[1].ul] = a[1].len;
  for (i = 2; i <= n; ++i) {
    for (j = 0; j < 26; ++j) dp[i][j] = dp[i - 1][j];
    for (pr = 0; pr < 26; ++pr) {
      dp[i][a[i].ul] = max(dp[i][a[i].ul], maxx[pr][a[i].pr] + a[i].len);
      maxx[pr][a[i].ul] = max(maxx[pr][a[i].ul], maxx[pr][a[i].pr] + a[i].len);
    }
    dp[i][a[i].ul] = max(dp[i][a[i].ul], a[i].len);
    maxx[a[i].pr][a[i].ul] = max(maxx[a[i].pr][a[i].ul], a[i].len);
  }
  for (i = 0; i < 26; ++i) {
    sol = max(sol, maxx[i][i]);
  }
  if (sol < 0)
    cout << "0";
  else
    cout << sol;
  return 0;
}
