N = int(input()) S1 = [input().strip() for _ in range(N)] S = [] for i in range(N): flag = 0 for j in range(len(S1[i])-1): if S1[i][j]>S1[i][j+1]: flag = 1 break if flag==0: S.append(S1[i]) S = sorted(S) M = len(S) if M==0: print(0) else: dp = [[0 for _ in range(26)] for _ in range(M)] for j in range(26): if chr(j+97)==S[0][-1]: dp[0][j] = len(S[0]) dmax = [0]*26 dmax[0] = dp[0][0] for j in range(1,26): dmax[j] = max(dmax[j-1],dp[0][j]) for i in range(1,M): dmax1 = [0]*26 for j in range(26): dp[i][j] = dp[i-1][j] if S[i][-1]==chr(j+97): dp[i][j] = max(dp[i][j],dmax[ord(S[i][0])-97]+len(S[i])) if j==0: dmax1[0] = dp[i][0] else: dmax1[j] = max(dmax1[j-1],dp[i][j]) dmax = dmax1[:] print(max(dp[M-1]))