#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <sstream>
#include <utility>

using namespace std;

typedef long long ll;
#define dbg(x) cerr << x << endl

int main()
{
    //ll n = 1;

    //dbg("N = " << n);
    string digits = "0123456789";

    /*for(int i = 0; i < 10; ++i)
    {
        digits.insert(make_pair(i, i));
    }*/

    stringstream ss;
    string str;

    getline(cin, str);

    ll N;

    cin >> N;

    char t;

    ss << str;

    while(ss >> t)
    {

        digits.erase(digits.find(t), 1);
        //dbg("a\n");
    }

    ll d;

    ll base = digits.length();
    ll result = 0;

    ll cbase = 1;

    while(N != 0)
    {
        d = N%10;
        N /= 10;

        result += digits.find(d+'0')*cbase;
        cbase *= base;

    }

    cout << result << endl;

    return 0;
}
