#include <bits/stdc++.h>
#define ll long long
#define pb push_back
using namespace std;
const int N = 10;
vector<int> good;
bool used[10];
string s, n;
int kol[10], nn, step[N], pos[N];

signed main()
{
    getline(cin, s);
    cin >> n;
    if (n == "0")
    {
        cout << 0;
        return 0;
    }
    for (int i = 0; i < s.size(); i++)
        if (s[i] == ' ')
            continue;
        else
            used[s[i]-'0'] = 1;
    for (int i = 0; i < 10; i++)
        if (!used[i])
            good.pb(i);
    kol[1] = good.size();
    int p = good.size();
    for (int i = 2; i < 10; i++)
    {
        if (!used[0])
            kol[i] = p*(good.size()-1);
        else
            kol[i] = p*good.size();
        p *= good.size();
    }
    step[0] = 1;
    for (int i = 1; i < 9; i++)
        step[i] = step[i-1]*good.size();
    int k = 0;
    for (int i = 1; i < n.size(); i++)
        k += kol[i];
    int num = 0;
     p = 0;
    for (int i = 0; i < good.size(); i++)
        pos[good[i]] = i+1;
    if (used[0])
        num += step[n.size()-1]*(pos[n[0]-'0']-1);
    else
        num += step[n.size()-1]*(pos[n[0]-'0']-2);
    for (int i = 1; i < n.size(); i++)
        num += step[n.size()-i-1]*(pos[n[i]-'0']-1);
    cout << num+k;
    return 0;
}
