n=int(input()) s=[] for i in range(n): x=input() now=x[0] for i in range(1,len(x)): if ord(now) > ord(x[i]): break now=x[i] else: s.append(x) s.sort() print(s) dp=[[-1]*27 for i in range(len(s)+1)] dp[0][0]=0 for i in range(len(s)): for j in range(27): if dp[i][j] == -1: continue dp[i+1][j] = max(dp[i][j],dp[i+1][j]) t = ord(s[i][0])-96 if t >= j:#当末尾と候補文字先頭を比べる t2 = ord(s[i][-1])-96#候補文字末尾の場所に移動する dp[i+1][t2] = max(dp[i+1][t2],dp[i][j]+len(s[i])) print(max(dp[-1]))