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;
class Program
{
    static void Main(string[] args)
    {
            // 入力
            string[] sInp = Console.ReadLine().Trim().Split(' ');
            int iN = int.Parse(sInp[0]);
            int iK = int.Parse(sInp[1]);
            sInp = Console.ReadLine().Trim().Split(' ');
            List<int> lstDes = new List<int>();                     // 嫌いな一覧
            for (int i = 0; i < iK; i++)
            {
                lstDes.Add(int.Parse(sInp[i]));                     // 嫌いな一覧
            }

            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            sw.Start();

            // 準備
            List<int> lstLik = new List<int>();                     // 好きな一覧
            for (int i = 0; i < 10; i++)
            {
                if (!lstDes.Contains(i)) { lstLik.Add(i); }         // 好きな一覧
            }
            List<ClsCmp> lstCmp = new List<ClsCmp>();               // 嫌いなものの変換先
            for (int i = 0; i < lstDes.Count; i++)
            {
                Boolean bEx = false;
                for (int j = 0; j < lstLik.Count; j++)
                {
                    if (lstLik[j] > lstDes[i]) { lstCmp.Add(new ClsCmp() { iDes = lstDes[i], iOKn = lstLik[j], iAdd = 0 }); bEx = true; break; }
                }
                if (!bEx) { lstCmp.Add(new ClsCmp() { iDes = lstDes[i], iOKn = lstLik[0], iAdd = 10 }); }       // 変換後桁上がりあり
            }

            // 解析
            int iLen = iN.ToString().Length;
            int iket = 0;
            while (true)
            {
                iket += 1;                                                                      // 桁位置
                if (iket > iN.ToString().Length) { break; }
                int iT = int.Parse(iN.ToString().Substring(iN.ToString().Length - iket, 1));    // 下の桁から確認
                for (int j = 0; j < lstCmp.Count; j++)
                {
                    if (lstCmp[j].iDes == iT)                                                   // 嫌いな値
                    {
                        iN += lstCmp[j].iAdd * (int)Math.Pow(10, iket - 1);                     // 桁上りあれば上の桁+1
                        char[] sN = iN.ToString().ToArray();                                    // 該当桁を変換するにはchar[]に
                        string sTmp = lstCmp[j].iOKn.ToString();                                // charに
                        sN[iN.ToString().Length - iket] = sTmp[0];                              // 該当桁を好きな値に変換
                        iN = int.Parse(new String(sN));                                         // 数値に戻す
                        break;
                    }
                }
            }

            // 出力
            Console.WriteLine("{0}", iN.ToString());

            sw.Stop();
#if DEBUG
            Console.WriteLine();
            Console.WriteLine("{0}", sw.Elapsed.ToString());
            Console.WriteLine("続行するには何かキーを押してください．．．");
            Console.ReadKey();
#endif
    }

        private class ClsCmp
        {
            public int iDes { get; set; } = 0;
            public int iOKn { get; set; } = 0;
            public int iAdd { get; set; } = 0;
        }
}