N = int(input()) S = [str(input()) for _ in range(N)] def getIndex(s): return ord(s)-96 l = [] for i in range(N): a,b = getIndex(S[i][0]),getIndex(S[i][-1]) if a <= b: l.append((a,b,len(S[i]))) l.sort(key=lambda x: x[2]) l.sort(key=lambda x: x[1]) l.sort(key=lambda x: x[0]) N = len(l) dp = [[0 for _ in range(27)] for _ in range(N+1)] for i in range(N): a,b,c = l[i] for j in range(27): dp[i+1][j] = max(dp[i][j], dp[i+1][j]) for j in range(a+1): dp[i+1][b] = max(dp[i][j]+c,dp[i+1][b]) ans = 0 for i in range(N+1): for j in range(27): ans = max(ans,dp[i][j]) # print(l) # for d in dp: # print(*d) print(ans)