N = int(input()) S = [input() for _ in range(N)] S.sort() dp = {} for c in 'abcdefghijklmnopqrstuvwxyz': dp[c] = -1 dp[chr(ord('a')-1)]=0 for l in S: #print("*", l) if ''.join(sorted(l)) != l: continue lc = l[-1] for c in reversed('abcdefghijklmnopqrstuvwxyz'): if dp[c] == -1: continue if lc < c: continue #print(c, dp[lc]) dp[lc] = max(dp[lc], dp[c] + len(l)) #print(c, dp[lc]) dp[lc] = max(dp[lc], len(l)) print(max(dp.values()))