# import sys # input = sys.stdin.readline def mp():return map(int,input().split()) def lmp():return list(map(int,input().split())) import math import bisect from copy import deepcopy as dc from itertools import accumulate from collections import Counter, defaultdict, deque def ceil(U,V):return (U+V-1)//V def modf1(N,MOD):return (N-1)%MOD+1 inf = int(1e30) mod = 998244353 n = int(input()) s = [] for i in range(n): si = input() for j in range(len(si)-1): if ord(si[j]) > ord(si[j+1]): break else:s.append(si) s.sort() dp = [[0]*26 for i in range(len(s)+1)] for i in range(1,len(s)+1): tar = s[i-1] front = tar[0] bottom = tar[-1] for j in range(ord(front)-ord("a")+1): dp[i][ord(bottom)-ord("a")] = max(dp[i-1][j]+len(tar), dp[i][ord(bottom)-ord("a")]) for j in range(26): dp[i][j] = max(dp[i - 1][j], dp[i][j]) print(max(dp[len(s)]))