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 main() {
  int n;
  cin >> n;
  string* s = new string[n];
  for (int i = 0; i < n; i++) {
    string str;
    cin >> str;
    s[i] = str;
  }
  int* t1 = new int[n];
  int* t2 = new int[n];
  int* t3 = new int[n];
  int* t4 = new int[n];
  fflush(stdin);
  int x = 0, y = 0, p = 0, q = 0;
  int len = s[0].length(), flag = 0;
  if (s[0][0] == s[0][len - 1]) {
    p = 0;
    x = len;
    q = 0;
    y = len;
    flag = 1;
  } else {
    p = 0;
    x = len;
    q = 0;
    y = len;
  }
  t1[0] = p;
  t2[0] = x;
  t3[0] = q;
  t4[0] = y;
  int ans = 0;
  for (int i = 1; i < n; i++) {
    x = 0;
    y = 0;
    p = i;
    q = i;
    flag = 0;
    len = s[i].length();
    x = len;
    if (s[i][0] == s[i][len - 1]) {
      y = len;
      flag = 1;
    }
    for (int j = 0; j < i; j++) {
      if (s[j][s[j].length() - 1] == s[i][0]) {
        if (t2[j] + len >= x) {
          x = t2[j] + len;
          p = t1[j];
        }
      }
      if (s[t3[j]][0] == s[i][len - 1] && s[j][s[j].length() - 1] == s[i][0]) {
        if (t4[j] + len >= y) {
          y = t4[j] + len;
          q = t3[j];
          flag = 1;
        }
      }
    }
    t1[i] = p;
    t2[i] = x;
    t3[i] = q;
    t4[i] = y;
    if (flag == 1 && ans < t4[i]) {
      ans = t4[i];
    }
  }
  cout << ans;
  delete[] t1;
  delete[] t2;
  delete[] t3;
  delete[] t4;
  return 0;
}
