import sys readline = sys.stdin.readline def alp_to_num(cha): num = ord(cha) - ord("a") return num N = int(readline()) D = [[0] * 26 for i in range(26)] for i in range(N): S = list(readline().rstrip()) S = list(map(alp_to_num, S)) if S != sorted(S): continue a, b = S[0], S[-1] if a == b: D[a][a] += len(S) else: D[a][b] = max(D[a][b], len(S)) for i in range(26): for j in range(i + 1, 26): D[i][j] += D[i][i] dp = [0] * 26 for i in range(1, 26): dp[i] = D[0][i] for i in range(1, 26): for j in range(i + 1, 26): dp[j] = max(dp[j], dp[i] + D[i][j]) dp[-1] += D[-1][-1] print(max(dp))