n = int(input()) s = [] for _ in range(n): t = input() ok = True for i in range(len(t) - 1): if t[i] > t[i + 1]: ok = False break if ok: a = ord(t[0]) - ord('a') b = ord(t[-1]) - ord('a') s.append((a, b, len(t))) s.sort() dp = [0] * 27 for a, b, l in s: for i in range(a + 1)[::-1]: dp[b] = max(dp[b], dp[i] + l) print(max(dp))