import math

d = [int(n) for n in input().split()]
n = input()

src = []
for i in list(range(10)):
    if i not in d:
        src.append(i)

out = list(range(len(src)))

r = len(src)

s = 0
k = len(n) - 1
for j in n:
    new = out[src.index(int(j))]
    s += math.pow(r, k) * new
    k = k - 1

print(int(s))
