N = int(input()) Ss = [input() for _ in range(N)] X = [[0] * 26 for _ in range(26)] for S in Ss: NS = len(S) for i in range(NS - 1): if S[i] > S[i + 1]: continue s0 = ord(S[0]) - 97 s1 = ord(S[-1]) - 97 if s0 == s1: X[s0][s1] += NS else: X[s0][s1] = max(X[s0][s1], NS) dp = [0] * 26 for i in range(26): for j in range(i, 26): if i == j: dp[i] += X[i][j] else: dp[j] = max(dp[j], dp[i] + X[i][j]) print(dp[-1])