import sys input = sys.stdin.readline sys.setrecursionlimit(10 ** 7) N = int(input()) cand = [] for _ in range(N): S = [ord(s) - 97 + 1 for s in input().rstrip()] L = len(S) ok = True for i in range(L-1): if S[i] > S[i+1]: ok = False break if not ok: continue cand.append((S[0], S[-1], L)) cand.sort() dp = [0] * 27 for s, t, l in cand: ndp = dp.copy() for x in range(s+1): ndp[t] = max(ndp[t], dp[x] + l) dp = ndp print(max(dp))