from bisect import bisect_right n = int(input()) memo = [[] for _ in range(26)] dp = [0] * 26 atode = 0 for _ in range(n): s = list(input()) if sorted(s) == s: num1 = ord(s[0]) - ord("a") num2 = ord(s[-1]) - ord("a") if num1 == num2: atode += len(s) else: memo[num1].append((num2, len(s))) for i in range(26): dp[i] = max(dp[: (i + 1)]) for a, b in memo[i]: dp[a] = max(dp[a], dp[i] + b) # print(dp) print(max(dp) + atode)