# dp[a] = アルファベットaが最後に来たときの最大の長さ N = int(input()) dp = [0] * 26 s = [input() for i in range(N)] s = sorted(s) for S in s: ok = True for i in range(len(S) - 1): if ord(S[i]) > ord(S[i + 1]): ok = False break if not ok: continue new_dp = dp.copy() start = ord(S[0]) - ord("a") end = ord(S[-1]) - ord("a") L = len(S) # start以前の文字から遷移可能。遷移先はend for c in range(start + 1): new_dp[end] = max(new_dp[end], dp[c] + L) dp = new_dp print(max(dp))