import collections N = int(input()) S = [list(input()) for _ in range(N)] tmp = collections.defaultdict(int) for s in S: if not s == sorted(s): continue key = (s[0], s[-1]) size = len(s) if s[0] == s[-1]: tmp[key] += size else: tmp[key] = max(tmp[key], size) dp = [0] * 26 for a in range(26): for b in range(a): dp[a] = max(dp[a], dp[b] + tmp[(chr(b + 97), chr(a + 97))]) dp[a] += tmp[(chr(a+97), chr(a+97))] print(dp[-1])