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;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ABC042C___こだわり者いろはちゃん
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> allNumList = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            string[] input = Console.ReadLine().Split(' ');
            //1桁ずつチェックするため
            char[] price = input[0].ToCharArray();
            int hateNumCount = int.Parse(input[1]);
            string[] hateNums = Console.ReadLine().Split(' ');
            string output = "";

            List<int> hateNumList = new List<int>();
            foreach (string hateNum in hateNums)
            {
                hateNumList.Add(int.Parse(hateNum));
            }

            // 使用可能な数字（嫌いでない数字）の抽出
            List<int> likeNumList = allNumList.Except(hateNumList).ToList();

            //数値探索

            //pattern 1
            //概要：使用可能な数字の最小値をを上位桁から決定していく
            //問題：桁あふれに対応できてない
            //      1)8889(8)             => 88888
            //      2)   9(2,3,4,5,6,7,8) => 22
            //      3)1234(0,1)           => 10000
            #region pattern 1
            //foreach (char priceNum in price)
            //{
            //    //使用可能な数字のうち、対照桁の数字以上で最小値を抽出

            //    int minLikeNum = likeNumList.Where(x => x >= int.Parse(priceNum.ToString())).Min();
            //    output += minLikeNum.ToString();
            //}
            #endregion

            //pattern 2
            //概要：使用可能な数字の最小値を下位桁から決定していく
            //      対象の桁の数字を超える数字が存在しない場合は使用可能数字の最小値　
            #region pattern 2
            //最大桁部が不足してたらもう一つ上位桁をまわす必要あり
            bool notExistOverDigit = true;
            foreach (char priceNum in price.Reverse())
            //char[] rPrice = price.Reverse().ToArray();
            //for (int index = 0; index < rPrice.Count(); index++)
            {
                //使用可能な数値のうち、対象桁の数値を上回る数値が存在するかチェック
                bool existOverNum = likeNumList.Any(x => x >= int.Parse(priceNum.ToString()));

                //使用可能な数字のうち、対照桁の数字以上で最小値を抽出
                int minLikeNum = likeNumList.Min();
                //存在しなければ要素が存在しないとエラー→boolで返却できるメソッドは？
                //Where→Any
                if (existOverNum&&notExistOverDigit)
                {
                    minLikeNum = likeNumList.Where(x => x >= int.Parse(priceNum.ToString())).Min();
                }
                else
                {
                    notExistOverDigit = false;
                }

                output = minLikeNum.ToString() + output;

            }
            //1回でも使用可能数値を上回る桁が存在すれば、
            if (!notExistOverDigit)
            {
                //このminLikeNumは0以外の使用可能桁の最小値
                output = likeNumList.Where(x => x != 0).Min().ToString() + output;
            }

            #endregion

            Console.WriteLine(output);

        }
    }
}