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# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;

class Program
{
    //支払金額
    static System.Text.StringBuilder N;
    //使用可能な数字のリスト
    static int[] likeNumbers;

    static void Main(string[] args)
    {
        Console.Write(IrohasObsession());
    }

    private static string IrohasObsession()
    {
        readData();

        Payment();

        return N.ToString();
    }

    /// <summary>
    /// 繰り上がり処理をする
    /// </summary>
    private static void roundUpProcess(int r)
    {
        bool roundUp = true;

        while (roundUp == true)
        {
            //最上位が繰り上げの場合
            if (r == 0)
            {
                if(likeNumbers[0] != 0) N.Insert(0, likeNumbers[0]);
                else N.Insert(0, likeNumbers[1]);

                break;
            }

            r--;
            N[r] = changeLikeNumber(N[r], ref roundUp);
        }
    }

    /// <summary>
    /// 変更桁以下を最小の使用可能な数で埋める
    /// </summary>
    /// <param name="r">変更桁</param>
    private static void changeLowOrderDigits(int r)
    {
        for (int i = r + 1; i < N.Length; i++)
            N[i] = (char)(likeNumbers[0]+'0');

    }

    /// <summary>
    /// 最大の変更桁を探して変更
    /// </summary>
    private static void Payment()
    {
        bool roundUp = false;

        for (int i = 0; i < N.Length; i++)
        {
            //使用可能数に含まれていない場合
            if (Array.IndexOf(likeNumbers, N[i] - '0') <= 0)
            {
                N[i] = changeLikeNumber(N[i] - '0', ref roundUp);

                //変更桁以下は0埋め
                changeLowOrderDigits(i);

                //くり上げフラグの処理
                if (roundUp == true) roundUpProcess(i);

                break;
            }
        }
    }

    /// <summary>
    /// 値をより大きい使用可能数にする
    /// </summary>
    /// <param name="v">調べる値</param>
    /// <param name="roundUp">くり上げフラグ</param>
    /// <returns></returns>
    private static char changeLikeNumber(int v, ref bool roundUp)
    {
        //入力値より大きい使用可能な数を探す
        for (int i = 0; i < likeNumbers.Length; i++)
        {
            if (likeNumbers[i] > v)
            {
                //繰り上げなし
                roundUp = false;
                return (char)(likeNumbers[i] + '0');
            }
        }

        //なければ繰り上げフラグを立て、最も小さい使用可能な数を返す
        roundUp = true;
        return (char)(likeNumbers[0]+'0');
    }

    /// <summary>
    /// データを読み込む
    /// </summary>
    private static void readData()
    {
        string[] line = Console.ReadLine().Split(' ');

        //支払金額N
        N = new System.Text.StringBuilder();

        N.Append(line[0]);

        //使用不可能な数字の個数K
        int K = int.Parse(line[1]);

        //使用可能な数字のリストを作る
        line = Console.ReadLine().Split(' ');

        likeNumbers = new int[10 - K];

        for (int i = 0, j = 0; i < 10; i++)
        {
            if (Array.IndexOf(line, i.ToString()) < 0)
            {
                likeNumbers[j] = i;
                j++;
            }
        }
    }
}