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 idx[26][26];
char buf[100];
vector<vector<int> > g;
map<string, int> M;
vector<string> names;
vector<int> res;
vector<int> used;
int ANS;
vector<int> tres[26];
vector<int> tpos[26];
vector<int> IT;
void dfs(int v, int ans) {
  used[v] = 1;
  int r = names[v][int((names[v]).size()) - 1] - 'a';
  ans += int((names[v]).size());
  res[v] = ans;
  for (int i = (0); i < (int((g[r]).size())); ++i) {
    if (used[g[r][i]] == 2) {
      if (g[r][i] <= v) continue;
      used[g[r][i]] = 1;
      for (int q = (0); q < (26); ++q) {
        if (tres[q][g[r][i]] != 0) {
          dfs(tpos[q][g[r][i]],
              ans + tres[q][g[r][i]] + int((names[g[r][i]]).size()));
        }
      }
      used[g[r][i]] = 2;
      continue;
    }
    if (used[g[r][i]] == 1) {
      if (g[r][i] <= v) {
        if (tres[r][g[r][i]] < ans - res[g[r][i]]) {
          tpos[r][g[r][i]] = i;
          tres[r][g[r][i]] = ans - res[g[r][i]];
        }
        ANS = max(ANS, ans - res[g[r][i]] + int((names[g[r][i]]).size()));
      } else
        dfs(g[r][i], ans);
      continue;
    }
    if (g[r][i] > v && used[g[r][i]] == 0) dfs(g[r][i], ans);
  }
  used[v] = 2;
}
int main() {
  int n;
  scanf("%d", &n);
  IT.resize(26, 0);
  ANS = 0;
  g.resize(26);
  int it = 0;
  names.resize(n);
  for (int i = (0); i < (26); ++i) {
    tres[i].resize(n, 0);
    tpos[i].resize(n, 0);
  }
  res.resize(n, 0);
  used.resize(n, 0);
  for (int i = (0); i < (n); ++i) {
    scanf("%s", buf);
    string s = buf;
    M[s] = it;
    names[it] = s;
    g[s[0] - 'a'].push_back(it);
    it++;
  }
  for (int i = (0); i < (n); ++i) {
    if (!used[i]) dfs(i, 0);
  }
  cout << ANS << endl;
  return 0;
}
