N = int(input()) S = [input() for i in range(N)] # dp[X] = アルファベットXまで進んだときの最大得点 # あらかじめソートしておく S = sorted(S) dp = [0] * 26 def char_to_num(x): return ord(x) - ord('a') for i in range(N): ok = True for j in range(len(S[i]) - 1): if S[i][j] > S[i][j + 1]: ok = False break if not ok: continue start = char_to_num(S[i][0]) end = char_to_num(S[i][-1]) length = len(S[i]) dp[end] = max(dp[end], max(dp[:start + 1]) + length) print(max(dp))