N = int(input()) L = [] for i in range(N): S = list(input()) if ''.join(map(str, S)) == ''.join(map(str, sorted(S))): L.append(S) L.sort() A = [0]*26 for l in L: begin = ord(l[0])-97 end = ord(l[-1])-97 A[end]=max(A[end],max(A[:begin+1])+len(l)) print(max(A))