import sys N = int(input()) l = [] for _ in range(N): S = input() T = list(S) if sorted(T) == T: l.append((T[-1],T[0],len(T))) now = 'a' ans = 0 l.sort() if len(l) == 0: print(0) exit() for e,s,length in l: if now <= s: ans += length now = e print(ans)