def s2m(s): ans = 36**12 for n in range(2,37): tmp = 0 for i,c in enumerate(s): z = int(c) if c.isdigit() else (ord(c)-55) if z >= n: break tmp += z*n**i else: ans = min(ans,tmp) return ans N = input() print min(s2m(raw_input()[::-1]) for i in range(N))