badNumbers = list(map(int,input().split()))
badNumbers.sort()
n = input()
k = 0
goodNumbers = dict()


if(not 0 in badNumbers):
    for i in range(10):
        if not i in badNumbers:
            goodNumbers[str(i)] = str(k)
            k += 1
    for i in range(0,len(n)):
        n = n[:i:] + goodNumbers[n[i]] + n[i+1::]
        
    print(int(n,len(goodNumbers)))
else:
    badNumbers.pop(0)
    for i in range(10):
        if not i in badNumbers:
            goodNumbers[str(i)] = str(k)
            k += 1
    for i in range(0,len(n)):
        if(goodNumbers[n[i]] != '0'):
            n = n[:i:] + goodNumbers[n[i]] + n[i+1::]
    print(int(n,len(goodNumbers)-1))
        
