def resolve(): n = int(input()) chars = ord("z") - ord("a") + 1 dp = [0] * (chars) next_dp = dp[:] l = [input() for _ in range(n)] for s in sorted(l): lens = len(s) if list(s) != sorted(list(s)): continue head = ord(s[0]) - ord("a") tail = ord(s[-1]) - ord("a") for i in range(tail): next_dp[i] = dp[i] for i in range(tail, chars): next_dp[i] = max(dp[i], dp[head] + lens) dp = next_dp[:] print(dp[-1]) if __name__ == '__main__': resolve()