#include<cstring>
#include<string>
#include<vector>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<stack>
#include<queue>
#include<cmath>
#include<algorithm>
#include<list>
#include<set>
#include<map>
#include<complex>
#include<sstream>
#include<climits>
#define rep(X,Y) for (int (X) = 0;(X) < (Y);++(X))
#define rrep(X,Y) for (int (X) = (Y)-1;(X) >=0;--(X))
#define all(X) (X).begin(),(X).end()
#define pb push_back
#define mp make_pair
#define fi first
#define sc second
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};

int N, K;
int D[10];
int a[10];

int main() {
  cin >> N >> K;
  rep(i,K) cin >> D[i];
  int t = 0;
  for (int i = 0; i < 10; ++i) {
    bool judge = true;
    for (int j = 0; j < K; ++j) {
      if (D[j] == i) judge = false;
    }
    if (judge) a[t++] = i;
  }

  int digit[4];
  for (int i = 0; i < 4; ++i) {
    digit[i] = N % 10;
    N /= 10;
  }


  if (digit[3] != 0) {

    int k3 = lower_bound(a,a+t,digit[3]) - a;
    int k2 = lower_bound(a,a+t,digit[2]) - a;
    int k1 = lower_bound(a,a+t,digit[1]) - a;
    int k0 = lower_bound(a,a+t,digit[0]) - a;
    while (k3 < t) {
      if (a[k3] > digit[3]) {
        k2 = 0; k1 = 0; k0 = 0;
      }
      while (k2 < t) {
        if (a[k2] > digit[2]) {
          k1 = 0; k0 = 0;
        }
        while (k1 < t) {
          if (a[k1] > digit[1]) {
            k0 = 0;
          }
          if (k0 < t) {
            cout << a[k3]*1000 + a[k2]*100 + a[k1]*10 + a[k0] << endl;
            return 0;
          }
          ++k1;
          k0 = 0;
        }
        ++k2;
        k1 = 0;
        k0 = 0;
      }
      ++k3;
      k2 = 0;
      k1 = 0;
      k0 = 0;
    }

    if (a[0] == 0) cout << a[1]*10000 << endl;
    else cout << a[0]*10000 + a[0]*1000 + a[0]*100 + a[0]*10 + a[0] << endl;

  } else if (digit[2] != 0) {

    int k2 = lower_bound(a,a+t,digit[2]) - a;
    int k1 = lower_bound(a,a+t,digit[1]) - a;
    int k0 = lower_bound(a,a+t,digit[0]) - a;
    while (k2 < t) {
      if (a[k2] > digit[2]) {
        k1 = 0; k0 = 0;
      }
      while (k1 < t) {
        if (a[k1] > digit[1]) {
          k0 = 0;
        }
        if (k0 < t) {
          cout << a[k2]*100 + a[k1]*10 + a[k0] << endl;
          return 0;
        }
        ++k1;
        k0 = 0;
      }
      ++k2;
      k1 = 0;
      k0 = 0;
    }

    if (a[0] == 0) cout << a[1]*1000 << endl;
    else cout << a[0]*1000 + a[0]*100 + a[0]*10 + a[0] << endl;

  } else if (digit[1] != 0) {

    int k1 = lower_bound(a,a+t,digit[1]) - a;
    int k0 = lower_bound(a,a+t,digit[0]) - a;
    while (k1 < t) {
      if (a[k1] > digit[1]) {
        k0 = 0;
      }
      if (k0 < t) {
        cout << a[k1]*10 + a[k0] << endl;
        return 0;
      }
      ++k1;
      k0 = 0;
    }

    if (a[0] == 0) cout << a[1]*100 << endl;
    else cout << a[0]*100 + a[0]*10 + a[0] << endl;

  } else {

    int k0 = lower_bound(a,a+t,digit[0]) - a;
    if (k0 < t) {
      cout << a[k0] << endl;
      return 0;
    }

    if (a[0] == 0) cout << a[1]*10 << endl;
    else cout << a[0]*10 + a[0] << endl;

  }

  return 0;
}



Measured memory_bytes: 256