Problem:
Score : 300 points

Problem Statement

Iroha is very particular about numbers. There are K digits that she dislikes: D_1, D_2, ..., D_K.

She is shopping, and now paying at the cashier.
Her total is N yen (the currency of Japan), thus she has to hand at least N yen to the cashier (and possibly receive the change).

However, as mentioned before, she is very particular about numbers. When she hands money to the cashier, the decimal notation of the amount must not contain any digits that she dislikes. Under this condition, she will hand the minimum amount of money.

Find the amount of money that she will hand to the cashier.

Constraints

1 ≦ N < 10000

1 ≦ K < 10

0 ≦ D_1 < D_2 < … < D_K≦9

\{D_1,D_2,...,D_K\} ≠ \{1,2,3,4,5,6,7,8,9\}

Input

The input is given from Standard Input in the following format:

N K
D_1 D_2 … D_K

Output

Print the amount of money that Iroha will hand to the cashier.

Sample Input 1

1000 8
1 3 4 5 6 7 8 9

Sample Output 1

2000

She dislikes all digits except 0 and 2.

The smallest integer equal to or greater than N=1000 whose decimal notation contains only 0 and 2, is 2000.

Sample Input 2

9999 1
0

Sample Output 2

9999

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<set>
#include<string>
#include<map>
#include<string.h>
#include<complex>
#include<math.h>
#include<queue>
#include <functional>
#include<time.h>
#include <stack>
#include<iomanip>
using namespace std;
#define rep(i,a,n) for(int i=(a);i<(n);i++)
#define ll long long
#define llint long long int
#define reverse(v)  reverse(v.begin(), v.end());
#define Yes(ans) if(ans)cout<<"Yes"<<endl; else cout<<"No"<<endl;
#define YES(ans) if(ans)cout<<"YES"<<endl; else cout<<"NO"<<endl;
#define hei(a) vector<a>
#define whei(a) vector<vector<a>>
#define UF  UnionFind 
#define Pint pair<int,int>
#define keta(a)  fixed << setprecision(a)
constexpr auto INF = 1000000000;
constexpr auto mod = 1000000007;
//辞書順はnext_permutation( begin( v ), end( v ) );やで！
llint gcd(llint a, llint b) {
	if (b > a)swap(a, b);
	if (a % b == 0)return b;
	return gcd(b, a % b);
}
llint Tow(int m,int n) {//2のn乗
	if (n == 0)return 1;
	llint x = Tow(m,n / 2);
	x *= x;
	if (n % 2 == 1)x *= m;
	return x;
}
ll max(ll a, ll b) {
	if (a < b)return b;
	else return a;
}

int SSS(string s) {
	int n = s.size();
	if (n > 4) {
		string t = "0";
		int x = 5;
		rep(i, 5, n + 1) {
			x = i;
			t = s.substr(n - i, i);
			if (stoi(t) > 2019)break;
		}
		if (t != "0") {
			int T = stoi(t) % 2019;
			t = t.substr(0, x);
			rep(i, 0, n - x)t += "0";
			s = SSS(t + to_string(T));
		}
	}
	if (stoi(s) == 0)return 0;
	if (stoi(s) % 2019 == 0)return 1;
	return 0;
}

int main() {
	hei(int) s;
	int n,k;
	cin >> n >> k;
	hei(int) a(k);
	rep(i, 0, k)cin >> a[i];
	rep(i, 0, 10) {
		bool S=true;
		rep(j, 0, k) {
			if (i == a[j])S = false;
		}
		if (S)s.push_back(i);
	}
	int x = s.size();
	int size = to_string(n).size();
	stack<string> st;
	st.push("");
	int ans = 0;
	while(st.size()!=0) {
		string now = st.top();
		st.pop();
		if (size<=now.size()) {
			int sum = stoi(now);
			if (sum >= n) {
				ans = sum;
				goto D;
			}
			for (int i = x - 1; i >= 0; i--) {
				string next = now + to_string(s[i]);
				st.push(next);
			}
		}

		else {
			for (int i = x-1; i >= 0; i--) {
				string next = now + to_string(s[i]);
				cout << next << endl;
				st.push(next);
			}
		}
	}
D:
	cout << ans << endl;
	return 0;
}