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.math.BigInteger;
import java.util.ArrayList;

import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
import java.util.Vector;


public class Sourcecode {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int numberofkings = input.nextInt();
        String[] kings = new String[numberofkings+1];
        for(int i=1;i<=numberofkings;i++){
           kings[i] = input.next();
        }
        GetAnswer(kings,numberofkings);
        
    }

    private static void GetAnswer(String[] kings, int numberofkings) {
        int[][] opt = new int[numberofkings+1][numberofkings+1];
        for(int i=1;i<=numberofkings;i++){
            for(int j=1;j<=numberofkings;j++){
            opt[i][j]=-1;   
            }
        }
        for(int i=1;i<=numberofkings;i++){
            for(int j=i;j<=numberofkings;j++){
                calculate(i,j,kings,numberofkings,opt);
            }
        }
        int max=-1;
        
        for(int i=1;i<=numberofkings;i++){
            for(int j=i;j<=numberofkings;j++){
                if(i==j&&kings[i].charAt(0)==kings[i].charAt(kings[i].length()-1)){
                    opt[i][j]=kings[i].length();
                }
                if(opt[i][j]>max&&kings[i].charAt(0)==kings[j].charAt(kings[j].length()-1)){
                max=opt[i][j]; //System.out.println("max="+max+"i="+i+",j="+j);
                }
                
            }
        }
        if(max==-1) max=0;
        System.out.println(max);
    }
//Opt(i,j) means the max length of consecutive kings start from number i and end with number j
    private static int calculate(int i, int j, String[] kings,
            int numberofkings, int[][] opt) {
        if(opt[i][j]!=-1) return opt[i][j];
        if(j<i) {opt[i][j]=0;return 0;}
        if(j==i){ opt[i][i]=kings[i].length(); return kings[i].length();}
        int max=-1;
        for(int t=i;t<=j;t++){
            for(int w=j;w>=t;w--){
                if(t==i&&w==j) continue;
                if(calculate(t,w,kings,numberofkings,opt)>max){
                    //System.out.println(calculate(t,w,kings,numberofkings,opt));
                    //System.out.println("t="+t+",w="+w);
                    //System.out.println("i="+i+",end with "+kings[i].charAt(kings[i].length()-1));
                    //System.out.println("t="+t+",start from "+kings[t].charAt(0));
                    
                    //System.out.println("w="+w+",end with "+kings[w].charAt(kings[w].length()-1));
                    //System.out.println("j="+j+",start from "+kings[j].charAt(0));
                    
                    
                    if(t!=i&&w!=j&&kings[i].charAt(kings[i].length()-1)==kings[t].charAt(0)&&kings[w].charAt(kings[w].length()-1)==kings[j].charAt(0))
                    {
                    //System.out.println("hi");
                        max=calculate(t,w,kings,numberofkings,opt);
                    //System.out.println("tmpmax1="+max);
                    
                    }
                    else if(kings[i].charAt(kings[i].length()-1)==kings[t].charAt(0)&&w==j){
                        max=calculate(t,w,kings,numberofkings,opt);
                        max=max-kings[w].length();
                        //System.out.println("tmpmax2="+max);
                    }
                    
                    else if(kings[w].charAt(kings[w].length()-1)==kings[j].charAt(0)&&i==t){
                        max=calculate(t,w,kings,numberofkings,opt);
                        max=max-kings[i].length();
                        //System.out.println("tmpmax3="+max);
                    }

                }
            }
        }
        if(max!=-1){ max= kings[i].length()+kings[j].length()+max;}
        else{max=-2;}
        opt[i][j]=max;
        //System.out.println("ATTENTION!i:"+i+",j:"+j+"，result:"+max);
        return max;
    }

    

}


    
