mod = 1000000007 eps = 10**-9 def main(): import sys input = sys.stdin.readline N = int(input()) T = [] for _ in range(N): S = input().rstrip('\n') S = [s for s in S] SS = sorted(S) if S == SS: T.append((S[0], S[-1], len(S))) for i in range(25): T.append((chr(i+97), chr(i+98), 0)) T.sort(key=lambda x: x[0]) dp = [0] * 26 for a, b, l in T: aa = ord(a) - 97 bb = ord(b) - 97 dp[bb] = max(dp[bb], dp[aa] + l) print(max(dp)) if __name__ == '__main__': main()