n = int(input()) from collections import defaultdict d = defaultdict(int) S = [] d["A"] = 0 for _ in range(n): s = list(input()) S.append([s,s[0],s[-1]]) S.sort(key=lambda x:(x[2],x[1])) for _ in range(n): s,xx,yy = S[_] if s != sorted(s): continue d_k = list(d.keys()) for k in d_k: if s[0] < k: pass else: d[s[-1]] = max(d[k] + len(s),d[s[-1]]) d[s[-1]] = max(len(s),d[s[-1]]) print(max(d.values()))