import sys def I(): return int(sys.stdin.readline().rstrip()) def MI(): return map(int,sys.stdin.readline().rstrip().split()) def LI(): return list(map(int,sys.stdin.readline().rstrip().split())) def LI2(): return list(map(int,sys.stdin.readline().rstrip())) def S(): return sys.stdin.readline().rstrip() def LS(): return list(sys.stdin.readline().rstrip().split()) def LS2(): return list(sys.stdin.readline().rstrip()) N = I() words = [(S(),i) for i in range(N)] words.sort() dic = {} for i in range(N): dic[words[i][1]] = i LCP = [0]*(N-1) for i in range(N-1): count = 0 for w0,w1 in zip(words[i][0],words[i+1][0]): if w0 == w1: count += 1 else: break LCP[i] = count class sparse_table(): def __init__(self,A,func): # 0-index self.N = len(A) self.func = func self.e = self.N.bit_length()-1 self.table = [[0]*self.N for _ in range(self.e+1)] self.table[0] = A[:] for j in range(self.e): k = 1 << j for i in range(self.N-2*k+1): self.table[j+1][i] = self.func(self.table[j][i],self.table[j][i+k]) def query(self,l,r): # [l,r) f = (r-l).bit_length()-1 return self.func(self.table[f][l],self.table[f][r-(1< j: i,j = j,i else: j += 1 di = dic[i-1] dj = dic[j-1] if di > dj: di,dj = dj,di ans += ST.query(di,dj) x = (x+d) % (N*(N-1)) print(ans)