import sys import io, os input = sys.stdin.readline n = int(input()) S = [input().rstrip() for i in range(n)] X = [0]*26 T = [] for s in S: temp = [] for c in s: temp.append(ord(c)-ord('a')) if sorted(temp) != temp: continue T.append((temp[0], temp[-1], len(s))) if not T: print(0) exit() T.sort() for p, q, l in T: X[q] = max(X[q], max(X[0:p+1])+l) print(max(X))