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 nextInt() {
  int x;
  scanf("%d", &x);
  return x;
}
long long nextLong() {
  long long x;
  scanf("%I64d", &x);
  return x;
}
double nextDouble() {
  double x;
  scanf("%lf", &x);
  return x;
}
const int BUFSIZE = 1000000;
char buf[BUFSIZE + 1];
string nextString() {
  scanf("%s", buf);
  return buf;
}
struct Point {
  double x, y;
  Point() : x(0), y(0) {}
  Point(double x, double y) : x(x), y(y) {}
  Point operator-(Point op) const { return Point(x - op.x, y - op.y); }
  Point operator+(Point op) const { return Point(x + op.x, y + op.y); }
  Point operator*(double op) const { return Point(x * op, y * op); }
  double operator*(Point op) const { return x * op.x + y * op.y; }
  double operator%(Point op) const { return x * op.y - y * op.x; }
  double length2() { return x * x + y * y; }
  double lengt() { return sqrt(length2()); }
};
Point nextPoint() {
  double x = nextDouble();
  double y = nextDouble();
  return Point(x, y);
}
void dfs(vector<vector<int> >& g, int n, vector<bool>& used, char f,
         vector<pair<string, int> >& v, int& m, int len) {
  vector<int>& line = g[n];
  for (int i = 0; i < (int)line.size(); ++i) {
    int j = line[i];
    if (!used[j]) {
      used[j] = true;
      int totalLen = len + v[j].second;
      if (f == v[j].first[1]) {
        m = max(m, totalLen);
      }
      dfs(g, j, used, f, v, m, totalLen);
    }
  }
}
int main() {
  int n = nextInt();
  vector<pair<string, int> > v(n);
  for (int i = 0; i < n; ++i) {
    string s = nextString();
    int len = s.length();
    string sh;
    char front = s[0];
    char end = s[0];
    if (len > 1) {
      end = s[len - 1];
    }
    sh.append(1, front);
    sh.append(1, end);
    v[i] = make_pair(sh, len);
  }
  vector<vector<int> > g(n);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (j != i) {
        if (v[i].first[1] == v[j].first[0]) {
          g[i].push_back(j);
        }
      }
    }
  }
  int m = 0;
  for (int i = 0; i < n; ++i) {
    vector<bool> used(n, false);
    used[i] = true;
    char first = v[i].first[0];
    int len = v[i].second;
    if (first == v[i].first[1]) {
      m = max(m, len);
    }
    dfs(g, i, used, first, v, m, len);
  }
  cout << m << endl;
  return 0;
}
