N = int(input()) S = [] for _ in range(N): s = input() l = list(s) if l == sorted(l): S.append(s) S.sort(key=lambda x: x[0]+x[-1]) DP = [[0] * 26 for _ in range(len(S)+1)] for i, s in enumerate(S): e = ord(s[-1])-ord('a') n = DP[i][ord(s[0])-ord('a')] + len(s) for j in range(e): DP[i+1][j] = DP[i][j] for j in range(e, 26): DP[i+1][j] = max(DP[i][j], n) print(DP[len(S)][25])