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>
int diru[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dirv[] = {-1, 0, 1, -1, 1, -1, 0, 1};
using namespace std;
struct debugger {
  template <typename T>
  debugger& operator,(const T& v) {
    cerr << v << " ";
    return *this;
  }
} dbg;
template <class T>
T sq(T n) {
  return n * n;
}
template <class T>
T gcd(T a, T b) {
  return (b != 0 ? gcd<T>(b, a % b) : a);
}
template <class T>
T lcm(T a, T b) {
  return (a / gcd<T>(a, b) * b);
}
template <class T>
bool inside(T a, T b, T c) {
  return a <= b && b <= c;
}
template <class T>
void setmax(T& a, T b) {
  if (a < b) a = b;
}
template <class T>
void setmin(T& a, T b) {
  if (b < a) a = b;
}
template <class T>
T power(T N, T P) {
  return (P == 0) ? 1 : N * power(N, P - 1);
}
int dp[500010];
bool isV[500010];
int pre[500010][26];
int len[500010];
int f[500010];
int s[500010];
char tmp[100];
int tar;
int cal(int i) {
  if (i < 0) return -(1 << 29);
  if (isV[i]) return dp[i];
  int ret;
  ret = max(len[i] + cal(pre[i + 1][s[i]]), cal(pre[i + 1][f[i]]));
  if (s[i] == tar) ret = max(ret, len[i]);
  isV[i] = 1;
  return dp[i] = ret;
}
int main() {
  int n, T, t = 1, m, i, j, k;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%s", tmp);
    len[i] = strlen(tmp);
    f[i] = tmp[0] - 'a';
    s[i] = tmp[len[i] - 1] - 'a';
  }
  memset(pre, -1, sizeof(pre));
  for (i = n - 1; i > -1; i--) {
    for (j = 0; j < 26; j++) {
      if (j == f[i])
        pre[i][j] = i;
      else
        pre[i][j] = pre[i + 1][j];
    }
  }
  int ans = 0;
  for (i = 0; i < 26; i++) {
    tar = i;
    memset(isV, 0, sizeof(isV));
    ans = max(ans, cal(pre[0][i]));
  }
  printf("%d\n", ans);
  return 0;
}
