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

import java.io.*;
import java.util.*;
public final class dynasty_puzzle
{
    static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
	static FastScanner sc=new FastScanner(br);
    static PrintWriter out=new PrintWriter(System.out);
	static int[][] dp;
	
    public static void main(String args[]) throws Exception
    {
		int n=sc.nextInt();String[] arr=new String[n+1];dp=new int[27][27];boolean one=false;int[][] a=new int[n+1][11];int[] len=new int[n+1];
		for(int i=1;i<=n;i++)
		{
			arr[i]=sc.next();len[i]=arr[i].length();
		}
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=len[i];j++)
			{
				a[i][j]=arr[i].charAt(j-1)-'a';
			}
		}
		int max=0;
		// dp[i][j]=max len of sequence having starting i and the last picked string's last char being j...
		for(int i=1;i<=n;i++)
		{
			if(a[i][1]==a[i][len[i]])
			{
				max=Math.max(max,len[i]);
			}
			int curr=dp[a[i][len[i]]][a[i][1]];
			if(curr!=0)
			{
				max=Math.max(max,curr+len[i]);
			}
			for(int j=0;j<26;j++)
			{
				if(dp[j][a[i][1]]!=0)
				{
					dp[j][a[i][len[i]]]=Math.max(dp[j][a[i][len[i]]],dp[j][a[i][1]]+len[i]);
				}
			}
			dp[a[i][1]][a[i][len[i]]]=Math.max(dp[a[i][1]][a[i][len[i]]],len[i]);
		}
		out.println(max);out.close();
    }
}
class FastScanner
{
    BufferedReader in;
    StringTokenizer st;

    public FastScanner(BufferedReader in) {
        this.in = in;
    }
	
    public String nextToken() throws Exception {
        while (st == null || !st.hasMoreTokens()) {
            st = new StringTokenizer(in.readLine());
        }
        return st.nextToken();
    }
	
	public String next() throws Exception {
		return nextToken().toString();
	}
	
    public int nextInt() throws Exception {
        return Integer.parseInt(nextToken());
    }

    public long nextLong() throws Exception {
        return Long.parseLong(nextToken());
    }

    public double nextDouble() throws Exception {
        return Double.parseDouble(nextToken());
    }
}