n = int(input()) s = [] for _ in range(n): t = input() f = 1 prev = 96 for c in t: if ord(c) < prev: f = 0 break prev = ord(c) if f: s.append(t) s.sort(key = lambda x: (ord(x[-1]), ord(x[0]))) dp = [0] * 26 for t in s: st = ord(t[0]) - 97 ed = ord(t[-1]) - 97 dp[ed] = max(dp[ed], max(dp[:st+1]) + len(t)) print(max(dp))