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: Accepted)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {

    public static void main(String[] args) throws Exception {
	    List<String> inputs = Io.read();
	    int[] metas = parse(inputs.get(0).split(" "));
	    int target = metas[0];

	    int[] hateNums = parse(inputs.get(1).split(" "));
	    List<Integer> validNums = validNumsInitialize(hateNums);

	    Calculator calc = new Calculator(target);

	    Io.write(calc.calc(validNums));
    }

    private static int[] parse(String[] arr) {
        int[] result = new int[arr.length];
        for(int i = 0; i < arr.length; i++) {
            result[i] = Integer.parseInt(arr[i]);
        }
        return result;
    }

    private static List<Integer> validNumsInitialize(int[] hateNums) {
        List<Integer> validNums = new ArrayList<>(Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));

        for (Integer num : hateNums) {
            validNums.remove(num);
        }

        return validNums;
    }
}

class Calculator {

    private int target;
    private int[] result;

    private int logNum;

    public Calculator(int target) {
        this.target = target;

        logNum = (int)Math.log10(target);
        result = new int[logNum + 2];
    }

    public int calc(List<Integer> validNums) {
        initialize(validNums);

        int before = getAnswer();
        int current = before;
        while(current >= target) {
            decrementTargetDigit(1, validNums);

            current = getAnswer();
            if (current < target) {
                break;
            }
            before = current;
        }

        return before;
    }

    private void initialize(List<Integer> validNums) {
        for(int i = logNum; i >= 0; i--) {
            result[i] = validNums.get(0);
            int num = (int)(target % Math.pow(10, i + 1)) / (int)Math.pow(10, i);
            for (int valid : validNums) {
                if (valid >= num) {
                    result[i] = valid;
                    break;
                }
            }
            if (i == logNum && result[logNum] == 0) {
                if (validNums.get(0) > 0) {
                    result[logNum + 1] = validNums.get(0);
                } else {
                    result[logNum + 1] = validNums.get(1);
                }
            }
        }
        while (getAnswer() < target) {
            incrementTargetDigit(1, validNums);
        }
    }

    private int getAnswer() {
        int ans = 0;
        for (int i = 0; i < result.length; i++) {
            ans += result[i] * Math.pow(10, i);
        }
        return ans;
    }

    private void decrementTargetDigit(int digitNum, List<Integer> validNums) {
        int num = result[digitNum - 1];
        int idx = validNums.indexOf(num);
        if (idx > 0) {
            result[digitNum - 1] = validNums.get(idx - 1);
        } else {
            if (digitNum == result.length - 1) {
                result[digitNum] = 0;
                result[digitNum - 1] = 0;
            } else if (digitNum >= result.length){
                result[digitNum - 1] = 0;
            } else {
                result[digitNum - 1] = validNums.get(validNums.size() - 1);
                decrementTargetDigit(digitNum + 1, validNums);
            }
        }
    }

    private void incrementTargetDigit(int digitNum, List<Integer> validNums) {
        int num = result[digitNum - 1];
        int idx = validNums.indexOf(num);
        if (idx < validNums.size() - 1) {
            result[digitNum - 1] = validNums.get(idx + 1);
        } else {
            result[digitNum - 1] = validNums.get(0);
            incrementTargetDigit(digitNum + 1, validNums);
        }
    }
}

class Io {

    /**
     * 標準入力から1行ずつ受け取る
     * @return
     * @throws IOException
     */
    public static List<String> read() throws IOException
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        List<String> inputs = new ArrayList<String>();
        String line;
        while ((line = br.readLine()) != null) {
            if (line.equals(""))
                break;

            inputs.add(line);
        }
        br.close();
        return inputs;
    }

    /**
     * 標準出力
     * @param line
     */
    public static void write(int line)
    {
        System.out.println(line);
    }
}