#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<set>
#include<string>
#include<vector>
#include<ctime>
#include<stack>
using namespace std;
typedef long long ll;
#define mm(a,b) memset(a,b,sizeof(a))
const int maxn = 1e6 + 50;
int n, vis[10];
int dp[10][2];
int d[10], num;
int main()
{
	int k;
	int x;
	scanf("%d%d", &n, &k);
	for (int i = 0; i < k; ++i)
	{
		scanf("%d", &x);
		vis[x] = 1;
	}
	x = n;
	num = 1;
	while (x)
	{
		d[num++] = x % 10;
		x /= 10;
	}
	int a, b;
	k = 1;
	mm(dp, -1);
	dp[0][0] = dp[0][1] = 0;
	for (int i = 1; i < num; ++i)
	{
		a = 10;
		for (int j = 0; j < 10; ++j)
		{
			if (!vis[j])
			{
				a = min(a, j);
				b = j;
				if (j > d[i]) break;
			}
		}
		if (dp[i - 1][1] != -1 && b>=d[i])
		{
			if (!vis[d[i]]) dp[i][1] = dp[i - 1][1] + d[i] * k;
			else dp[i][1] = dp[i - 1][0] + b * k;
		}
		if (a <= d[i] && dp[i - 1][0] != -1) dp[i][0] = dp[i - 1][0] + a * k;
		k *= 10;
	}
	if (dp[num - 1][1] != -1)
		cout << dp[num - 1][1];
	else
	{
		for (int j = 0; j < 10; ++j)
		{
			if (!vis[j])
			{
				a = j;
				break;
			}
		}
		cout << dp[num - 1][0] + a * k;
	}
}