#int(input()) #map(int, input().split()) #list(map(int, input().split())) N = int(input()) S = [0] * N for i in range(N): S[i] = input() a = [] for i in range(N): t = "".join([str(x) for x in sorted(S[i])]) if t == S[i]: a.append((ord(S[i][0])-97, ord(S[i][-1])-97, len(S[i]))) # print(a[-1], S[i]) a = sorted(a) dp = [0] * 26 for i in range(len(a)): u, v, w = a[i] dp[v] = max(dp[v], max(dp[:u+1])+w) print(max(dp))