#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

 void print(vector<int> &vec)
{
    for (int i = 0; i < vec.size(); i++)
        cout << vec[i] << " ";
    //cout << endl;
}

int power(int exp)
{
    int result = 1;
    while (exp > 0)
    {
        result *= 10;
        exp --;
    }
    return result;
}

void increment(vector<int> &number, vector<int> &ex, int &count)
{
    for (int i = 0; i < number.size(); i++)
    {

        bool b = false;
        bool first = true;
        do
        {
            if (first == true)
                first = false;
            else
            {
                count += power(i);
            }
            number[i] ++;
            if (number[i] == 10)
            {
                b = true;
                number[i] = 0;
                if (i == number.size() - 1)
                    number.push_back(0);
            }
        }
        while (find(ex.begin(), ex.end(), number[i]) != ex.end());


        if (b == false)
            break;
    }
}

int main()
{
    string s;
    getline(cin, s);
    vector<int> ex;
    for (int i = 0; i < s.size(); i++)
        if (s[i] != ' ')
            ex.push_back(s[i] - '0');

    int n;
    cin >> n;
    int m = n;
    vector<int> N;
    while (n > 0)
    {
        N.push_back(n % 10);
        n /= 10;
    }


    vector<int> number;
    number.push_back(0);
    int count = 0;
    while (number != N)
    {
        increment(number, ex, count);
    }

    cout << m - count;

}
