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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
import java.util.*;
public class Main {
    public static void main(String []args){
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        Task solver=new Task();
        solver.solve(1,in,out);
        out.close();
    }
    static class Task{
        public void solve(int testnumber,InputReader in,PrintWriter out){
            int n = in.nextInt();
            int k = in.nextInt();
            int [] a = new int[k];
            int [] b = new int[10];
            for(int i=0;i<k;i++){
                a[i] = in.nextInt();
            }
            int u = n;
            int h = 0;
            while(u != 0){
               b[h++] = u % 10;
               u /= 10;
            }
            int pos = 0;
            for(int i = h - 1 ; i >= 0 ; i--){
                int num = b[i];
                boolean flag = false;
                boolean f = false;
                for(int j = 0 ;j < k ; j++){
                    if(a[j] == num) {
                        flag = true;
                        break;
                    }
                }
                if(flag){
                    int tempnum = b[i] + 1;
                    while(tempnum != 10){
                        boolean flag2 = false;
                        for(int j = 0 ; j < k ; j++){
                            if(a[j] == tempnum){
                                flag2 = true;
                                break;
                            }
                        }
                        if(flag2){
                            tempnum++;
                        } else {
                            f = true;
                            b[i] = tempnum;
                            break;
                        }
                    }
                }
                if(f){
                    pos = i;
                    break;
                }
            }
            for(int i = pos - 1 ; i >= 0 ; i--){
                int temp = 0;
                while(temp <= 9){
                    boolean flag10 = false;
                    for(int j = 0 ; j < k ; j++){
                        if(temp == a[j]){
                            flag10 = true;
                            break;
                        }
                    }
                    if(flag10){
                        temp++;
                    } else {
                        b[i] = temp;
                        break;
                    }
                }
            }
            for(int i = h -1 ; i >= 0 ; i--){
                out.print(b[i]);
            }
            out.println("");
        }
    }
    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;
        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }
        public int nextInt() {
            return Integer.parseInt(next());
        }
    }
}