N = int(input()) dat = [[0] * 26 for _ in range(26)] for _ in range(N): S = input() flag = True for i in range(len(S) - 1): if S[i] > S[i+1]: flag = False break if flag: c = ord(S[0]) - ord('a') cc = ord(S[-1]) - ord('a') if c == cc: dat[c][c] += len(S) else: dat[c][cc] = max(dat[c][cc],len(S)) dp = [0] * 26 dp[0] = dat[0][0] for i in range(26): for k in range(i+1,26): dp[k] = max(dp[k],dp[i] + dat[i][k] + dat[k][k]) print(max(dp))