def is_good(S): p="" for s in S: if p>s: return False p=s return True from string import ascii_lowercase as X N=int(input()) D={a:{b:0 for b in X} for a in X} for _ in range(N): S=input() if not is_good(S): continue if S[0]==S[-1]: D[S[0]][S[-1]]+=len(S) else: D[S[0]][S[-1]]=max(D[S[0]][S[-1]],len(S)) X_len=len(X) DP=[0]*X_len for i in range(X_len): a=X[i] for j in range(i): b=X[j] DP[i]=max(DP[i],DP[j]+D[b][a]+D[a][a]) print(max(DP))