stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include<bits/stdc++.h>  

#define Max(x,y) ((x)>(y)?(x):(y))  

#define Min(x,y) ((x)<(y)?(x):(y))  

#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();++it)  

#define Abs(x,y) ((x)>(y)?((x)-(y)):((y)-(x)))  

#define ll long long  

#define Mem0(x) memset(x,0,sizeof(x))  

#define Mem1(x) memset(x,-1,sizeof(x))  

#define MemX(x) memset(x,0x3f,sizeof(x))  

#define pb push_back  

using namespace std;  

const int N=20;  

bool flag[N];  

  

int main(){  

  int k,i,x,j;  

  char n[N],s[N];  

  while(~scanf("%s%d",n,&k)){  

    Mem0(flag);  

    for(i=0;i<k;++i){  

      scanf("%d",&x);  

      flag[x]=1;  

    }  

    int len=strlen(n),f1=0,f2=0;  

    for(i=0;i<len;++i){  

      if(f2){  

        for(j=0;j<=9;++j){  

          if(flag[j]==0 && j>n[i]-'0'){  

            s[i]='0'+j;  

            n[i]=s[i];  

            break;  

          }  

        }  

        if(j==10){  

          if(!i){ f1=1; break; }  

          i-=2,f2=1;  

          continue;  

        }else{  

          f2=i;  

          break;  

        }  

      }  

      if(flag[n[i]-'0']){  

        for(j=0;j<=9;++j){  

          if(flag[j]==0 && j>n[i]-'0'){  

            s[i]='0'+j;  

            n[i]=s[i];  

            break;  

          }  

        }  

        if(j==10){  

          if(!i){ f1=1; break; }  

          f2=1,i-=2;  

          continue;  

        }else{  

          f2=i;  

          break;  

        }  

      }else s[i]=n[i];  

    }  

    if(f1){  

      for(i=1;i<=9;++i)  

        if(flag[i]==0){  

          printf("%d",i);  

          break;  

        }  

      for(i=0;i<=9;++i)  

        if(flag[i]==0){  

          while(len--)printf("%d",i);  

          break;  

        }  

    }else{  

      if(i==len)printf("%s",n);  

      else{  

        for(i=0;i<=f2;++i)  

          printf("%c",s[i]);  

        for(j=0;j<=9;++j)  

          if(flag[j]==0){  

            for(;i<len;++i)  

              printf("%d",j);  

            break;  

          }  

      }  

    }  

    puts("");  

  }  

}  

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

#include<vector>

using namespace std;

int k,N;

bool vis[20];

bool Pan(int x)

{

	while(x>0) 

	{

		if(vis[x%10])

			return 0;

		x/=10;

	}

	return 1;

}

int main()

{

	scanf("%d%d",&N,&k);

	for(int i=1;i<=k;i++)

	{

		int x;

		scanf("%d",&x);

		vis[x]=1;

	}

	if(N==0&&vis[0])

	{

		printf("1");

		return 0;

	}

	for(int i=N;;i++)

		if(Pan(i))

		{

			printf("%d",i);

			return 0;

		}

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<cstdio>

int n,k,a;

bool C[15];



bool Check(int x)

{

	char s[10];

	sprintf(s+1,"%d",x);

	for(int i=1;s[i];i++)

		if(C[s[i]-'0'])

			return false;

	return true;

}



int main()

{

	scanf("%d %d",&n,&k);

	for(int i=1;i<=k;i++)

	{

		scanf("%d",&a);

		C[a]=1;

	}

	for(int i=n;;i++)

		if(Check(i))

		{

			printf("%d\n",i);

			break;

		}

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

#include<vector>

using namespace std;

int k,N;

bool vis[20];

bool Pan(int x)

{

	while(x>0) 

	{

		if(vis[x%10])

			return 0;

		x/=10;

	}

	return 1;

}

int main()

{

	scanf("%d%d",&N,&k);

	for(int i=1;i<=k;i++)

	{

		int x;

		scanf("%d",&x);

		vis[x]=1;

	}

	if(N==0&&vis[0])

	{

		printf("1");

		return 0;

	}

	for(int i=N;;i++)

		if(Pan(i))

		{

			printf("%d",i);

			return 0;

		}

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <map>

#include <cstdio>

#include <vector>

#include <sstream>

#include <iostream>

#include <algorithm>

#define LL long long

using namespace std;

const int MAXN = 2e3+55;

int vis[20];

int main()

{

    int n,k,v;

    cin>>n>>k;

    for(int i=1;i<=k;++i)

    {

        cin>>v;

        vis[v]++;

    }

    string s;

    for(int i = n;;++i)

    {

        stringstream in;

        in<<i;

        in>>s;

        bool flag = true;

        for(int i=0;i<s.size();++i)

        {

            int t = s[i] - '0';

            if(vis[t]){

                flag = false;

                break;

            }

        }

        if(flag) {cout<<i;return 0;}

    }

    return 0;

}


// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;



int n,k,x;

bool arr[10];



bool check(int n)

{

    while(n)

    {

        if(arr[n%10])

            return 0;

        n /= 10;

    }

    return 1;

}



int main()

{

    while(scanf("%d%d",&n,&k)!=EOF)

    {

        for(int i=0;i<k;i++)

        {

            scanf("%d",&x);

            arr[x] = 1;

        }

        for(int i=n;;i++)

        {

            if(check(i))

            {

                printf("%d\n",i);

                break;

            }

        }

    }

return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <stdio.h>

#include <string.h>

#include <iostream>

#include <algorithm>

#include <math.h>

#include <limits.h>

#include <map>

#include <stack>

#include <queue>

#include <vector>

#include <set>

#include <string>

#define ll long long

#define ull unsigned long long

#define ms(a) memset(a,0,sizeof(a))

#define pi acos(-1.0)

#define INF 0x7f7f7f7f

#define lson o<<1

#define rson o<<1|1

const double E=exp(1);

const int maxn=1e6+10;

const int mod=1e9+7;

using namespace std;

int vis[maxn];

int a[maxn];

int b[maxn];

int _;

bool init(int n)

{

	int res=0;

	if(n)

	{

		while(n)

		{

			b[res++]=n%10;

			n/=10;

		}

	}

	else

	{

		b[0]=0;

		res++;

	}

	for(int i=0;i<res;i++)

		for(int j=0;j<_;j++)

			if(b[i]==a[j])

				return false;

	return true;

}

int main(int argc, char const *argv[])

{

	ios::sync_with_stdio(false);

	int n,k;

	cin>>n>>k;

	int x;

	for(int i=0;i<k;i++)

	{

		cin>>x;

		vis[x]=1;

	}

	_=0;

	for(int i=0;i<10;i++)

	{

		if(vis[i])

			a[_++]=i;

	}

	for(int i=n;;i++)

	{

		if(init(i))

		{

			cout<<i<<endl;

			return 0;

		}

	}

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;



int n,k,x;

bool arr[10];



bool check(int n)

{

    while(n)

    {

        if(arr[n%10])

            return 0;

        n /= 10;

    }

    return 1;

}



int main()

{

    while(scanf("%d%d",&n,&k)!=EOF)

    {

        for(int i=0;i<k;i++)

        {

            scanf("%d",&x);

            arr[x] = 1;

        }

        for(int i=n;;i++)

        {

            if(check(i))

            {

                printf("%d\n",i);

                break;

            }

        }

    }

return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



using namespace std;



int main() {

#ifdef LOCAL

  freopen("input.txt", "r", stdin);

  freopen("output.txt", "w", stdout);

#endif

  ios::sync_with_stdio(false);

  cin.tie(0);

  int n, k;

  cin >> n >> k;

  vector<int> cant(10);

  for (int i = 0; i < k; ++i) {

    int d;

    cin >> d;

    cant[d] = 1;

  }

  vector<int> v;

  while (n > 0) {

    v.push_back(n % 10);

    n /= 10;

  }

  int smallest = -1;

  for (int i = 0; i < 10; ++i) {

    if (!cant[i]) {

      smallest = i;

      break;

    }

  }

  assert(smallest != -1);

  int dig = (int) v.size();

  for (int i = dig - 1; i >= 0; --i) {

    if (cant[v[i]]) {

      int j = i, need = 1;

      while (j < dig && need) {

        for (int c = v[j] + 1; c <= 9; ++c) {

          if (!cant[c]) {

            v[j] = c;

            need = 0;

            break;

          }

        }

        if (need) {

          ++j;

        } else {

          break;

        }

      }

      if (j == dig && need) {

        int largest = smallest;

        if (largest == 0) {

          for (int k = smallest + 1; k < 10; ++k) {

            if (!cant[k]) {

              largest = k;

              break;

            }

          }

        }

        assert(largest != 0);

        v.push_back(largest);

      }

      for (int k = 0; k < j; ++k) {

        v[k] = smallest;

      }

      break;

    }

  }

  for (int i = (int) v.size() - 1; i >= 0; --i) {

    cout << v[i];

  }

  cout << '\n';

  return 0;

}


// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;



int n,k,x;

bool arr[10];



bool check(int n)

{

    while(n)

    {

        if(arr[n%10])

            return 0;

        n /= 10;

    }

    return 1;

}



int main()

{

    while(scanf("%d%d",&n,&k)!=EOF)

    {

        for(int i=0;i<k;i++)

        {

            scanf("%d",&x);

            arr[x] = 1;

        }

        for(int i=n;;i++)

        {

            if(check(i))

            {

                printf("%d\n",i);

                break;

            }

        }

    }

return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <cstdio>

#include <cstring>

#include <cmath>

#include <cstdlib>

#include <algorithm>

#include<iostream>

#include<vector>

using namespace std;

int main()

{

    int n,k;

    while(cin>>n>>k)

    {

        int q[10];

        int qq[100];

//        int mi=100;

        memset(qq,0,sizeof(qq));

        for(int i=1; i<=k; i++)

        {

            cin>>q[i];

            qq[q[i]]=1;

        }

        int sum=0;

        int a[6];

//        cout<<n<<endl;

        for(int j=n; j<=100000; j++)

        {

            int i=j;

            a[0]=i/10000;

            i%=10000;

            a[1]=i/1000;

            i%=1000;

            a[2]=i/100;

            i%=100;

            a[3]=i/10;

            a[4]=i%10;



//            if(i=10000)

//                cout<<a<<" "<<b<<" "<<c<<" "<<d<<endl;

            int flag=1;

            int vis=1;

            for(int i=0;i<=4;i++)

            {

                if(a[i]!=0)

                {

                    vis=0;

                }

                if(vis==0)

                {

                    if(qq[a[i]]==1)

                        flag=0;

                }

            }

            if(flag)

            {

                sum=j;

                break;

            }

        }

        cout<<sum<<endl;

    }

}


// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;



int n,k,x;

bool arr[10];



bool check(int n)

{

    while(n)

    {

        if(arr[n%10])

            return 0;

        n /= 10;

    }

    return 1;

}



int main()

{

    while(scanf("%d%d",&n,&k)!=EOF)

    {

        for(int i=0;i<k;i++)

        {

            scanf("%d",&x);

            arr[x] = 1;

        }

        for(int i=n;;i++)

        {

            if(check(i))

            {

                printf("%d\n",i);

                break;

            }

        }

    }

return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <algorithm>

#include <cmath>

#include <cstdio>

#include <vector>

#include <cstring>

#include <algorithm>

#define Inf 0x3f3f3f3f

using namespace std;

typedef long long ll;

int a[100010];

int vis[100010];

bool check(int n)

{

    while(n)

    {

        if(vis[n%10])

            return false;

        n/=10;

    }

    return true;

}

int main()

{

    int n,k;

    scanf("%d %d",&n,&k);

    memset(vis,0,sizeof(vis));

    for(int i=0; i<k; i++)

    {

        scanf("%d",&a[i]);

        vis[a[i]]=1;

    }

    for(int i=n; ; i++)

    {

        if(check(i))

        {

            printf("%d\n",i);

            return 0;

        }

    }

    return 0;

}


// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;



int n,k,x;

bool arr[10];



bool check(int n)

{

    while(n)

    {

        if(arr[n%10])

            return 0;

        n /= 10;

    }

    return 1;

}



int main()

{

    while(scanf("%d%d",&n,&k)!=EOF)

    {

        for(int i=0;i<k;i++)

        {

            scanf("%d",&x);

            arr[x] = 1;

        }

        for(int i=n;;i++)

        {

            if(check(i))

            {

                printf("%d\n",i);

                break;

            }

        }

    }

return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<cstring>

#include<cstdio>

#include<algorithm>

using namespace std;

int p[100005];

bool judge(int n)

{

    int b;

    int flag=1;

    while(n)

    {

        b=n%10;

        if(p[b])

        {

             return false;

        }

          n/=10;

    }

    return true;

}

int main()

{

    int N,K;

    int m;

    while(scanf("%d%d",&N,&K)!=EOF)

    {

        memset(p,0,sizeof(p));

        for(int i=0;i<K;i++)

        {

              scanf("%d",&m);

              p[m]=1;

        }

         for(int i=N;;i++)

         {

             if(judge(i))

             {

                 printf("%d\n",i);

                 break;

             }

         }

    }

    return 0;

}

// Optimized solution to the same problem (faster):
#include<iostream>

#include<cmath>

#include<cstring>

#include<cstdio>

#include<algorithm>

using namespace std;

int n,k,vis[10];

int check(int n){

	while(n){

		if(vis[n%10]==1)

		return 0;

		n/=10;

	}

    return 1;

	}

int main()

{

	cin>>n>>k;

	memset(vis,0,sizeof(vis));

	int a;

	for(int i=0;i<k;i++){

	cin>>a;

	vis[a]=1;

}

	int i=n;

	while(1){

		if(check(i)){

			cout<<i<<endl;

			break;

		}

		i++;

	}

	

	

 } 

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<string>

#include<sstream>

using namespace std;

int n, k, d[9];



bool solve(int n) {

	string s;

	stringstream ss;

	ss << n;

	ss >> s;

	for (int i = 0; i < k; i++) {

		if (s.find((char)(d[i] + '0')) != string::npos) return false;

	}

	return true;

}

int main() {

	cin >> n >> k;

	for (int i = 0; i < k; i++) cin >> d[i];

	while (n < 100000) {

		if (solve(n)) break;

		n++;

	}

	cout << n << endl;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

#define int long long

#define endl '\n'

#define FOR(i, a, n) for (int i = (a); i < (n); ++i)

#define REP(i, n) FOR(i, 0, n)

using namespace std;



int N, K;

set<int> st;



int dfs(int lev, int s) {

    if (s >= N) return s;

    int res = 1LL << 30;

    if (lev == 5) return res;

    for (int d : st) res = min(res, dfs(lev + 1, s * 10 + d));

    return res;

}



void _main() {

    cin >> N >> K;

    REP (i, 10) st.insert(i);

    REP (i, K) {

        int D;

        cin >> D;

        st.erase(D);

    }

    cout << dfs(0, 0) << endl;

}



signed main() {

    ios::sync_with_stdio(false);

    cin.tie(nullptr);

    cout << fixed << setprecision(10);

    _main();

    return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;



#define int long long

#define pb push_back

#define f first

#define s second

#define all(container) container.begin(),container.end()



#define fast ios::sync_with_stdio(0); cin.tie(0)



int32_t main(){

  

    int n,k; cin >> n >> k;

    bool dislike[11] = {false};

    for(int i=1;i<=k;i++){

      int d; cin >> d;

      dislike[d] = true;

    }



    while(true){

      string s = to_string(n);

      bool found = true;

      for(int i=0;i<s.size();i++){

        if(dislike[s[i]-'0']){

          found = false;

          break;

        } 

      }

      if(found){

        cout << n;

        break;

      } 

      else n++;

    }



    return 0;

} 

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;



#define int long long

#define pb push_back

#define f first

#define s second

#define all(container) container.begin(),container.end()



#define fast ios::sync_with_stdio(0); cin.tie(0)



int32_t main(){

  

    int n,k; cin >> n >> k;

    bool dislike[10] = {false};

    

    for(int i=1;i<=k;i++){

      int d; cin >> d;

      dislike[d] = true;

    }



    string s = to_string(n);

    int maxx_digit;



    for(int i=0;i<10;i++){

      if(!dislike[i]) maxx_digit = i;

    }



    for(int i=0;i<s.size();i++) s[i] = maxx_digit + 48;



    for(int i=s.size()-1;i>=0;i--){

      for(int j=0;j<10;j++){

        if(!dislike[j]){

          char temp = s[i];

          // cout << "j " << j << '\n';

          s[i] = j + 48;

          if(stoi(s)<n){

            s[i] = temp;

          }

          else break;

          // cout << "s = " <<./a s << '\n';

        }

      }

    }



    if(stoi(s)<n){

      vector<char> min_digit;

      for(int i=0;i<10;i++){

        if(!dislike[i]&&min_digit.size()!=2){

          min_digit.pb(i+48);

        }

      }



      s = string(s.size()+1,min_digit[0]);

      // cout << s;

      if(min_digit[0]=='0') s[0] = min_digit[1];



    }



    cout << s;



    return 0;

} 

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <vector>

#include <string>

using namespace std;

int main(void){

    // Here your code !

    int n,k;

    cin >> n >> k;

    vector<int> d(10,0);

    for(int i = 0; i < k; i++){

        int t;

        cin >> t;

        d[t] = 1;

    }

    

    for(int i = n; i < 1000000; i++){

        string s = to_string(i);

        int j;

        for(j = 0; j < s.length(); j++){

            if(d[s[j]-'0']){

                break;

            }

        }

        if(j == s.length()){

        cout << i << endl;

        break;

        }

    }

}


// Optimized solution to the same problem (faster):
#include <iostream>

#include <map>

#include <algorithm>

#include <string>

#include <vector> 

#include <numeric>

#include <cmath>

#include <queue>

#include <iomanip>

#include <functional>





#define CHMAX(a,b) a = std::max(a,b)

#define CHMIN(a,b) a = std::min(a,b)

#define CHABS(a) a = std::abs(a) 

#define COUT(a) std::cout << a << std::endl

#define CERR(a) std::cerr << a << std::endl

#define FOR(n) for(lli i = 0; i < n; i++)



using namespace std;

using lli = long long int;

using pll = pair<lli, lli>;

using tlll = tuple<lli, lli, lli>;

using vll = vector<lli>;



lli mod197 = 1000000007LL;

lli INF = 10000000000000;



// ax + by = gcd(a,b) 最大公約数

template< typename T >

T extgcd(T a, T b, T& x, T& y) {

	T d = a;

	if (b != 0) {

		d = extgcd(b, a % b, y, x);

		y -= (a / b) * x;

	}

	else {

		x = 1;

		y = 0;

	}

	return d;

}



lli D[10] = {};



int main(void) {



	lli N, K;

	cin >> N >> K;



	//N以上で制約を満たす最も小さい数

	for (int i = 0; i < K; i++) {

		lli t;

		cin >> t;

		D[t] = 1;

	}



	for (lli i = N; i <= 20 * N; i++) {

		lli t = i;

		bool f = true;

		while (t != 0) {

			if (D[t % 10]) {

				f = false;

				break;

			}

			t /= 10;

		}

		if (!f)

			continue;

		COUT(i);

		return 0;

	}







	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

int check(char s,int K,int D[]){

	for(int i=0;i<K;i++){

		char temp[2];

		temp[0] = D[i]+'0';

		if(s==temp[0]){

			return 0;

		}

	}

	return 1;

}

int main(void){

	int N,K;

	cin >> N >> K;

	int D[K];

	for(int i=0;i<K;i++)cin >> D[i];

	int ans = 0;

	for(int i=N;i<=999999;i++){

		//条件を満たしたら終了

		string tmp = to_string(i);

		int flag  =1;

		for(int j=0;j<tmp.size();j++){

			if(check(tmp[j],K,D)==0){

				flag=0;

			}

		}

		if(flag == 1){

			cout << i;

			return 0;

		}

	}

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

#define ALL(A) (A).begin(),(A).end()

#define ll long long

#define rep(i,n) for(int i=0;i<(n);i++)



const ll mod = 1e9+7;

const ll INF = 2*1e18;

const int inf = 1e9+7;



using namespace std;



int main(void){

	cin.tie(0);

	ios::sync_with_stdio(false);

	int N,K;

	cin >> N >> K;

	vector<bool> ok(10,1);

	int ans = N;

	rep(i,K){

		int a;

		cin >> a;

		ok[a] = 0;

	}

	bool ng = 1;

	ans = N-1;

	while(ng){

		ng = 0;

		ans++;

		int tmp = ans;

		while(tmp){

			int x = tmp%10;

			tmp/=10;

			if(ok[x]==0){

				ng=1;

				break;

			}

		}

	}

	cout << ans << endl;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#define fastio     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

#define all(x)     (x).begin() , (x).end()

#define rep(i,a,b) for(int i=a;i<b;i++)

#define endl       '\n'

#define MAX        1000000

#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)

typedef long long int ll;

using namespace std;



void __print(int x) {cerr << x;}

void __print(long x) {cerr << x;}

void __print(long long x) {cerr << x;}

void __print(unsigned x) {cerr << x;}

void __print(unsigned long x) {cerr << x;}

void __print(unsigned long long x) {cerr << x;}

void __print(float x) {cerr << x;}

void __print(double x) {cerr << x;}

void __print(long double x) {cerr << x;}

void __print(char x) {cerr << '\'' << x << '\'';}

void __print(const char *x) {cerr << '\"' << x << '\"';}

void __print(const string &x) {cerr << '\"' << x << '\"';}

void __print(bool x) {cerr << (x ? "true" : "false");}



template<typename T, typename V>

void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}

template<typename T>

void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}

void _print() {cerr << "]\n";}

template <typename T, typename... V>

void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}





const ll mod = 1e9+7;

const double eps = 1e-9;

const double pi = acos(-1);



void solve(){

    int k;

    int n;

    cin>>n>>k;

    bool arr[11];

    memset(arr,false,sizeof(arr));

    for(int i=0;i<k;i++) {

        int t;

        cin>>t;

        arr[t]=true;

    }

    for(int i=n;i<100000;i++) {

        string ans=to_string(i);

        for(int j=0;j<(int)ans.size();j++) {

            if(arr[ans[j]-'0'])goto E;

        }

        cout<<i<<"\n";break;E:;

    }

}











int main(){

    fastio

    int test=1;

    while(test--)

        solve();

    return 0;

}


// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

typedef long long int ll;

using namespace std;



int n, k;

int d[10];

void solve(){

    cin >> n >> k;

    for(int i=0;i<k;++i) {

        int  t;

        cin >> t;

        d[t] = 1;

    }

    bool fl = 1;

    auto ok = [&](int x) {

        while(x) {

            if(d[x%10])

                return 0;

            x/=10;

        }

        return 1;

    };

    while(fl) {

        if(ok(n))

            fl = 0;

        else 

            n ++;

    }

    cout << n << "\n";

}



int main(){

    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

    int t =1;

    while(t--)

        solve();

    return 0;

}




// Fastest known solution for this problem: