#include <bits/stdc++.h>
#define en "\n"
typedef long long ll;
typedef long double ld;
using namespace std;
bool nott[10];
int wh[10];
main()
{
    string s;
    getline(cin, s);
    for (int i=0; i<s.size(); i+=2){
        nott[s[i]-'0']=1;
    }
    int q=0;
    for (int i=0; i<10; ++i){
        if (!nott[i]){
            wh[i]=q;
            ++q;
        }
    }
    cin>>s;
    ll step=1;
    ll ans=0;
    for (int i=s.size()-1; i>-1; --i){
        ans+=wh[(s[i]-'0')]*step;
        step*=q;
    }
    cout<<ans<<en;

}
