#include <cstdio>
#include <sstream>
using namespace std;

char s[1000];
int i, j, k, n, m;
int bad[10], d[10];

int mypow(int x, int n) {
    int res = 1;
    for (int i = 0; i < n; i++) res *= x;
    return res;
}

int main() {
    gets(s);
    stringstream ss; ss << s; 
    m = 0;
    while (ss >> i) {
          m++;
          bad[i] = 1;
    }
    scanf("%d", &n);
    k = 0;
    while (n > 0) {
          d[k++] = n%10;
          n /= 10;
    }
    int ans = 0;
    for (i = k-1; i >= 0; i--) {
        for (j = 0; j < d[i]; j++) {
            if (i == k-1 && j == 0) continue;
            if (!bad[j]) {
                 ans = ans + mypow(10-m, i);        
            }
        }
    }
    for (i = k-1; i >= 1; i--)
        if (bad[0]) ans += mypow(10-m, i);
        else ans += (10-m-1)*mypow(10-m, i-1);
    if (k>1 || d[0]>0) if (!bad[0]) ans++;
    printf("%d\n", ans);
    
    return 0;
}
