import sys, math sys.setrecursionlimit(10**8) sys.set_int_max_str_digits(0) INF = 1e18 MOD = 998244353 from bisect import bisect_left, bisect_right from collections import deque, defaultdict, Counter from itertools import product, combinations, permutations, groupby, accumulate from heapq import heapify, heappop, heappush input = sys.stdin.readline def I(): return input().rstrip() def II(): return int(input().rstrip()) def chmax(DP,i,v): if DP[i] < v: DP[i] = v def chmin(DP,i,v): if DP[i] > v: DP[i] = v N = II() S = [] for i in range(N): s = I() # s が良い文字列か? pre = 0 for ss in s: now = ord(ss) - 97 if pre > now: break pre = now else: S.append((ord(s[0]) - 97, ord(s[-1]) - 97, len(s))) S.sort(key=lambda x: (x[0], x[1])) DP = [0] * 26 for src, dst, l in S: chmax(DP, dst, max(DP[:src + 1]) + l) print(max(DP))