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;
vector<vector<int>> DP;
vector<pair<int, int>> words;
vector<int> w;
vector<vector<int>> invw(26);
vector<int> inv2w;
vector<int> inv3w;
int f(int c, int i) {
  if (DP[c][i] != -1) return DP[c][i];
  int total = (int)-1e9;
  if (words[i].second == c) total = w[i];
  if (inv2w[i] < invw[words[i].second].size()) {
    total = max(total, f(c, invw[words[i].second][inv2w[i]]) + w[i]);
  }
  if (inv3w[i] < invw[words[i].first].size()) {
    total = max(total, f(c, invw[words[i].first][inv3w[i]]));
  }
  return DP[c][i] = total;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  string s;
  words.assign(n, make_pair(0, 0));
  w.assign(n, 0);
  DP.assign(26, vector<int>(n, -1));
  inv2w.assign(n, 0);
  inv3w.assign(n, 0);
  for (int i = 0; i < (int)n; i++) {
    cin >> s;
    words[i] = make_pair(s[0] - 'a', s[s.size() - 1] - 'a');
    w[i] = s.size();
    invw[s[0] - 'a'].emplace_back(i);
    inv2w[i] = invw[s[s.size() - 1] - 'a'].size();
    inv3w[i] = invw[s[0] - 'a'].size();
  }
  int M = 0;
  for (int i = 0; i < (int)n; i++) {
    M = max(M, f(words[i].first, i));
  }
  cout << M << '\n';
}
