using System;
using System.IO;
using System.Text;

#region Program
namespace Program
{
    #region Solver
    public class Solver
    {
        static IO.StreamScanner
        sc = new IO.StreamScanner(Console.OpenStandardInput());

        static readonly int KAZU = 10;
        static readonly char Z = '0';

        string N;
        int K;
        int[] D;

        int likeMin;
        int nextLikeMin;

        int num = 0;

        #region Solver
        public Solver()
        {
            N = sc.Scan();
            K = sc.Integer();

            D = new int[K];

            for (int i = 0; i < K; i++) D[i] = sc.Integer();

            // 好きな数の最小を取得
            likeMin = 0;
            for (int i = 0; i < D.Length; i++)
            {
                if (likeMin != D[i]) break;
                likeMin++;
            }
        }
        #endregion

        #region Solve
        public void Solve()
        {
            bool flg = false;
            int idxD = 0;
            int idxN = 0;

            // どの桁に嫌いな数字があるか
            for (int i = 0; i < N.Length; i++)
            {
                if (flg) break;
                idxD = searchIdxD(N[i].ToString());
                if (idxD != int.MinValue)
                {
                    flg = true;
                    idxN = i;
                }
            }

            // 嫌いな数がない
            if (!flg)
            {
                Console.WriteLine(N);
                return;
            }

            // お値段以上の好きな数のうち最小を取得し出力
            Console.WriteLine(getNextPrice(idxN, idxD));
            return;
        }
        #endregion

        #region searchIdxD
        private int searchIdxD(string str)
        {
            int ret = int.MinValue;

            for (int i = 0; i < K; i++)
            {
                int n = int.Parse(str);
                if (n == D[i])
                {
                    return i;
                }
            }

            return ret;
        }
        #endregion

        #region shiftCheck
        public bool shiftCheck(int n, int idxD)
        {
            for (int i = idxD; i < D.Length; i++)
            {
                int d = int.Parse(D[i].ToString());

                if (n == d)
                {
                    n++;
                    continue;
                }
                else
                {
                    break;
                }
            }
            num = n;

            if (num == KAZU) return true;
            return false;
        }
        #endregion

        #region getNextPrice
        public string getNextPrice(int idxN, int idxD)
        {
            string ret = string.Empty;

            // 最も左側にある嫌いな数を含む上位の数字
            string upper = N.Substring(0, idxN + 1);

            // 全桁数
            int tLen = N.Length;

            // 上位桁数
            int uLen = upper.Length;

            // 下位桁数
            int lLen = tLen - uLen;

            int n = int.Parse(N[idxN].ToString());

            for (int i = 0; i <= upper.Length; i++)
            {
                // 好きな数に到達するのに桁上がりの有無を判定
                if (!shiftCheck(n, idxD))
                {
                    // 桁上がり無し
                    return make(uLen - 1, num, makeSb(lLen));
                }
                else
                {
                    // 桁上がり有り
                    uLen--;
                    lLen++;

                    n = uLen <= 0 ? 1 : int.Parse(N[uLen - 1].ToString()) + 1;
                    idxD = searchIdxD(n.ToString());
                    if (idxD == int.MinValue)
                    {
                        return make(uLen, n, makeSb(lLen));
                    }
                }
            }
            return string.Empty;
        }
        #endregion

        #region
        private string make(int uLen, int num, StringBuilder sb)
        {
            string ret = string.Empty;

            if (uLen <= 0)
            {
                ret = num.ToString() + sb.ToString();
                return ret;
            }

            ret =
                N.Substring(0, uLen - 1)
                + num.ToString()
                + sb.ToString();

            return ret;
        }
        #endregion

        #region
        private StringBuilder makeSb(int n)
        {
            string strLikeMin = likeMin.ToString();
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < n; i++) sb.Append(strLikeMin);
            return sb;
        }
        #endregion
    }
    #endregion
}
#endregion

#region Execute
class Execute
{
    public static void Main(string[] args)
    {
        new Program.Solver().Solve();
        Program.IO.Printer.Out.Flush();
    }
}
#endregion

#region Program.IO
namespace Program.IO
{
    using System.Text;
    using System.Globalization;
    using System.Linq;
    public class Printer : StreamWriter
    {
        static Printer()
        {
            Out = new Printer(Console.OpenStandardOutput())
            {
                AutoFlush = false
            };
        }
        public static Printer Out
        {
            get;
            set;
        }
        public override IFormatProvider FormatProvider
        {
            get
            {
                return CultureInfo.InvariantCulture;
            }
        }
        public Printer(System.IO.Stream stream)
            : base(stream, new UTF8Encoding(false, true))
        {
        }
        public Printer(System.IO.Stream stream, Encoding encoding)
            : base(stream, encoding)
        {
        }
        public void Write<T>(string format, T[] source)
        {
            base.Write(format, source.OfType<object>().ToArray());
        }
        public void WriteLine<T>(string format, T[] source)
        {
            base.WriteLine(format, source.OfType<object>().ToArray());
        }
    }
    public class StreamScanner
    {
        public readonly Stream str;
        private readonly byte[] buf = new byte[1024];
        private int len, ptr;
        public bool isEof = false;
        public StreamScanner(Stream stream)
        {
            str = stream;
        }
        private T[] enumerate<T>(int n, Func<T> f)
        {
            var a = new T[n];
            for (int i = 0; i < n; ++i) a[i] = f();
            return a;
        }
        public long Long()
        {
            if (isEof)
            {
                return long.MinValue;
            }
            long ret = 0;
            byte b = 0;
            var ng = false;
            do
            {
                b = read();
            }
            while (b != '-' && (b < '0' || '9' < b));
            if (b == '-')
            {
                ng = true;
                b = read();
            }
            for (; true; b = read())
            {
                if (b < '0' || '9' < b)
                {
                    return ng ? -ret : ret;
                }
                else ret = ret * 10 + b - '0';
            }
        }
        public long[] Long(int n)
        {
            return enumerate(n, Long);
        }
        public int Integer()
        {
            return (isEof) ? int.MinValue : (int)Long();
        }
        public int[] Integer(int n)
        {
            return enumerate(n, Integer);
        }
        public char[] Char(int n)
        {
            return enumerate(n, Char);
        }
        public char Char()
        {
            byte b = 0;
            do
            {
                b = read();
            }
            while ((b < 33 || 126 < b) && !isEof);
            return (char)b;
        }
        public string Scan()
        {
            var sb = new StringBuilder();
            for (var b = Char(); b >= 33 && b <= 126; b = (char)read())
            {
                sb.Append(b);
            }
            return sb.ToString();
        }
        private byte read()
        {
            if (isEof)
            {
                return 0;
            }
            if (ptr >= len)
            {
                ptr = 0;
                if ((len = str.Read(buf, 0, 1024)) <= 0)
                {
                    isEof = true;
                    return 0;
                }
            }
            return buf[ptr++];
        }
    }
}
#endregion


Measured memory_bytes: 2648