n = int(input()) TMP = [[0 for _ in range(26)] for _ in range(26)] for _ in range(n): S = list(input()) if S != sorted(S): continue f = ord(S[0]) - ord('a') l = ord(S[-1]) - ord('a') if f == l: TMP[f][l] += len(S) else: TMP[f][l] = max(TMP[f][l], len(S)) DP = [0 for _ in range(26)] for i in range(26): DP[i] += TMP[i][i] for j in range(i + 1, 26): DP[j] = max(DP[j], DP[i] + TMP[i][j]) print(DP[25])