N = int(input()) todec = {chr(ord("A") + i): 10 + i for i in range(26)} for i in range(10): todec[str(i)] = i V = [input() for i in range(N)] ans = 10 ** 20 for i in range(N): val = 0 base = 0 for j in range(len(V[i])): base = max(base, todec[V[i][j]]) base += 1 for j in range(len(V[i])): val *= base val += todec[V[i][j]] ans = min(ans, val) print(ans)