stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#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("");  

  }  

}  

// Faster C++ solution for the same problem (measured speedup: 5.048182615530066x):
#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;

		}

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 1.7187339818432414x):
#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;

		}

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

int main() {

	int n, k;

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

	int d[k];

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

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

	bool used[10];

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

		used[i] = true;

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

		used[d[i]] = false;

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

		string s = to_string ( i );

		bool ok = true;

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

			if ( !used[s[j] - '0'] ) 

				ok = false;

		if ( ok ) {

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

			break;

		}

	}

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.1454463834197561x):
#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;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

int main() {

	int n, k;

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

	int d[k];

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

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

	bool used[10];

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

		used[i] = true;

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

		used[d[i]] = false;

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

		string s = to_string ( i );

		bool ok = true;

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

			if ( !used[s[j] - '0'] ) 

				ok = false;

		if ( ok ) {

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

			break;

		}

	}

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.1616846079373575x):
#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;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

int main() {

	int n, k;

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

	int d[k];

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

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

	bool used[10];

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

		used[i] = true;

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

		used[d[i]] = false;

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

		string s = to_string ( i );

		bool ok = true;

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

			if ( !used[s[j] - '0'] ) 

				ok = false;

		if ( ok ) {

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

			break;

		}

	}

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.1712068435075453x):
#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;

    }

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 1.874980117922049x):
#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;

    }

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 1.357601047898226x):
#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;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

int main() {

	int n, k;

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

	int d[k];

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

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

	bool used[10];

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

		used[i] = true;

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

		used[d[i]] = false;

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

		string s = to_string ( i );

		bool ok = true;

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

			if ( !used[s[j] - '0'] ) 

				ok = false;

		if ( ok ) {

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

			break;

		}

	}

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 6.051704427501756x):
#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;

}
// Slower C++ solution:
#define _CRT_SECURE_NO_WARNINGS

#pragma _attribute_((optimize("-O2")))



#include <iostream>

#include <queue>

#include <stack>

#include <cstdio>

#include <vector>

#include <map>

#include <list>

#include <set>

#include <cstdio>

#include <bitset>

#include <algorithm>

#include <cmath>

#include <cstring>

#include <cstdlib>

#include <string>

#include <sstream>

#include <time.h>

#include <iomanip>



//#define Local



using namespace std;



set<int> dis;



bool chs(int in)

{

	bool flag = 1;

	while(flag&&in>0)

	{

		int tmp = in % 10;

		in /= 10;

		if (dis.count(tmp) != 0)

			flag = 0;

	}

	return flag;

}



int main()



{



	ios::sync_with_stdio(false);

	cin.tie(0);

	//freopen("input1.txt", "r", stdin);

	//freopen("output1.txt", "w", stdout);



	int t, k;

	while(cin>>t>>k)

	{

		dis.clear();

		while(k--)

		{

			int tmp;

			cin >> tmp;

			dis.insert(tmp);

		}

		while(1)

		{

			if(chs(t))

			{

				cout << t << endl;

				break;

			}

			t++;

		}

	}



#ifdef Local

	cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;

#endif

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.244021635579267x):
#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;

}
// Slower C++ solution:
#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("");  

  }  

}  

// Faster C++ solution for the same problem (measured speedup: 5.041306097377169x):
#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;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 1.7163927619773462x):
#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;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 5.2832716704157345x):
#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;

}
// Slower C++ solution:
#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;

}


// Faster C++ solution for the same problem (measured speedup: 5.209421202753929x):
#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;

}