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

from collections import defaultdict
import sys

f = open(sys.argv[1]) if len(sys.argv) > 1 else sys.stdin

n = int(f.readline())
names = [line.strip() for line in f]

def ddint():
    return defaultdict(int)

alphabet = 'abcdefghijklmnopqrstuvwxyz'

d = [[0 for ch in alphabet] for ch in alphabet]
print d

for name in names:
    s, e = name[0], name[-1]
    s, e = ord(s) - ord('a'), ord(e) - ord('a')
    for i in range(len(alphabet)):
        if d[i][s] > 0:
            d[i][e] = max(d[i][e], d[i][s] + len(name))
    d[s][e] = max(d[s][e], len(name))

print max(d[i][i] for i in range(len(alphabet)))

'''names_startswith = defaultdict(list)
for i, name in enumerate(names):
    names_startswith[name[0]].append(i)

g = {}
for i, name in enumerate(names):
    g[i] = [index for index in names_startswith[name[-1]] if index > i]

#print g

cache = {}

def my_max(seq):
    res = 0
    for el in seq:
        if el > res:
            res = el
    return res

def max_path(start, first_letter):
    if (start, first_letter) in cache:
        return cache[(start, first_letter)]
    else:
        max_succ = my_max(max_path(succ, first_letter) for succ in g[start])

        if max_succ > 0:
            res = max_succ + len(names[start])
        elif names[start][-1] == first_letter:
            res = len(names[start])
        else:
            res = 0

        cache[(start, first_letter)] = res
        return res

_max = 0
for start in range(len(names)):
    m = max_path(start, names[start][0])
    if m > _max:
        _max = m

print _max
'''
