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 <vector>
//#include <string>
#include <algorithm>

using namespace std;

vector<int> pays;
void SolveCSub(int pay, int digit, const bool hate[10], int N){
  if(digit >= 100000){
    if(pay > N)
      pays.push_back(pay);
    return;
  }
  for(int i = 0; i < 10; i++){
    int newpay = pay;
    if(!hate[i]){
      newpay += digit * i;
      SolveCSub(newpay, digit*10, hate, N);
    }
  }
}

void SolveC(){
  bool hate[10];
  int N, K;
  cin >> N >> K;
  for(int i = 0; i < K; i++){
    int a;
    cin >> a;
    hate[a] = true;
  }

  SolveCSub(0,1,hate, N);

  sort(pays.begin(),pays.end());

  cout << pays[0] << endl;
}

//int dp[100001];
int main(){
  SolveC();
/*
  const int kDiv = 1000000007;
  int H, W, A, B;
  cin >> H >> W >> A >> B;

  dp[0] = 1;
  for(int y = 0; y < H-A; y++)
    for(int x = 0; x < W; x++)
      dp[x+1] = (dp[x+1] + dp[x]) % kDiv;

  for(int y = 0; y < A; y++)
    for(int x = B; x < W; x++)
      dp[x+1] = (dp[x+1] + dp[x]) % kDiv;
  cout << dp[W-1] << endl;
*/
  return 0;
}


/*
int main(){
  int N, L;
  cin >> N >> L;
  vector<string> strs(N);
  for(int i = 0; i < N; i++)
    cin >> strs[i];
  sort(strs.begin(), strs.end());

  for(int i = 0; i < N; i++)
    cout << strs[i];
  cout << endl;
  return 0;
}
*/

/*int main(int argc, const char * argv[]) {
  int count5 = 0;
  int count7 = 0;
  int a;
  for(int i = 0; i < 3; i++){
    cin >> a;
    if(a == 5) count5++;
    else if(a == 7) count7++;
    else break;
  }

  if(count5 == 2 && count7 == 1) cout << "YES" << endl;
  else cout << "NO" << endl;

  return 0;
}*/