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 <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <string.h>
#include <fstream>

using namespace std;


#define LL long long
#define ULL unsigned long long
#define N 1000220
#define M 5000100
#define MP make_pair
#define Pi acos(-1.0)
#pragma comment(linker,"/STACK:1024000000,1024000000")
#define ls (rt << 1)
#define rs (ls | 1)
#define md ((ll+rr)/2)
#define lson ll, md, ls
#define rson md+1, rr, rs
#define mod 1000000007
#define inf 0x3f3f3f3f
#define sqr(x) ((x)*(x))
#define eps 1e-6
#define MP make_pair
#define uLL unsigned long long
LL powmod(LL a,LL b) {LL res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
#define F(x) ((x)/3+((x)%3 == 1 ? 0 : tb))
#define G(x) ((x)<tb ? (x)*3+1 : ((x) - tb)*3+2)
#define lowbit(x) ((x)&(-x))
#define fi first
#define se second
#define pii pair<int,int>
#define B 100000001
const LL INF = ~0u >> 1;

int gcd(int x,int y){
    while(y){
        int t = x % y;
        x = y;
        y = t;
    }
    return x;
}

template<typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

const int mask = (1 << 26) - 1;
char s[N];

int vis[10],dig[10],ans[10];

int main(){
    srand(time(0));
    //freopen("in.txt", "r", stdin);
    //freopen("in.txt", "w", stdout);
    //cout<<20<<endl;
    //int T = 20;
    //scanf("%d",&T);
    
    int n,k;
    scanf("%d%d",&n,&k);
    
    for(int i = 0; i < k; i++){
        int j;
        scanf("%d",&j);
        vis[j] = 1;
    }
    
    int len = 0;
    while(n){
        dig[len++] = n % 10;
        ans[len-1] = dig[len-1];
        n /= 10;
    }
    int flag = 0;
    for(int i = len - 1; ~i; i--){
        
        if(flag){

            for(int j = 0; j <= 9; j++){
                if(!vis[j]){
                    ans[i] = j;
                    break;
                }
            }
            continue;
        }
        
        if(vis[dig[i]]){
            flag = 1;
            bool gao = 0;
            for(int j = dig[i] + 1; j <= 9; j++) if(!vis[j]){
                ans[i] = j;
                gao = 1;
                break;
            }
            if(gao) continue;
            
            for(int j = i; j < len; j++){
                if(gao) break;
                for(int k = 0; k <= 9; k++) {
                    if(!vis[k]) {
                        ans[i] = k;
                        break;
                    }
                }
                for(int k = dig[i+1]+1; i <= 9; i++)
                    if(!vis[k]){
                        ans[i+1] = k;
                        gao = 1;
                        break;
                    }
            }
        }
    }
    
    for(int i = len; ~i; i--){
        if(i != len)
            printf("%d",ans[i]);
        else if(ans[i])
            printf("%d",ans[i]);
    }
    puts("");
    
    return 0;
}