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 c not in dp: continue if l[0] < c: continue #print(c, dp[lc]) if lc not in dp: dp[lc] = dp[c] + len(l) else: dp[lc] = max(dp[lc], dp[c] + len(l)) #print(c, dp[lc]) if lc not in dp: dp[lc] = len(l) else: dp[lc] = max(dp[lc], len(l)) #print(l) #print(dp) print(max(dp.values()))