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