module app;

import core.bitop;

import std.algorithm;
import std.array;
import std.bigint;
import std.conv;
import std.stdio;
import std.string;
import std.traits;

struct Input
{
	int n, k;
	int[] d;
}

void parseInput(T)(out Input input, T file)
{
	with (file) with (input)
	{
		auto ar = readln().strip().split().map!(to!int).array();
		n = ar[0];
		k = ar[1];
		d = readln().strip().split().map!(to!int).array();
	}
}

auto main2(Input* input)
{
	with (input)
	{
		bool[10] like;
        like[] = true;
        foreach (dd; d)
            like[dd] = false;
        int minLikeGte(int i)
        {
            for (; i < like.length; i++)
                if (like[i])
                    return i;
            return -1;
        }

        int[5] price;
        price[] = -1;

        auto temp = n;
        for (int i = 4; i >= 0; i--)
        {
            price[i] = temp % 10;
            temp /= 10;
        }

        int[5] payment;
        payment[] = 0;

        LOOP_KETA:
        for (int keta = 0; keta < 5; keta++)
        {
            if (price[keta] == 0)
                continue;

            payment[keta] = minLikeGte(price[keta]);

            if (payment[keta] == price[keta])
                continue;

            for (int kuriage = keta; kuriage >= 0; kuriage--)
            {
                auto newValue = minLikeGte(price[kuriage] + 1);
                if (newValue != -1)
                {
                    payment[kuriage] = newValue;
                    payment[kuriage+1..$] = minLikeGte(0);
                    break LOOP_KETA;
                }
            }
        }

        int result = payment[0];
        for (size_t keta = 1; keta < 5; keta++)
        {
            result *= 10;
            result += payment[keta];
        }
        return result;
	}
}

alias retType = ReturnType!main2;
static if (!is(retType == void))
{
	unittest { writeln("begin unittest"); }
	auto _placeholder_ = ReturnType!main2.init;

	unittest // example1
	{
		string example =
`1000 8
1 3 4 5 6 7 8 9`;
		if (example.empty) return;
		Input input = void;
		parseExample(input, example);
		auto result = main2(&input);
		printResult(result);
		assert(result == 2000);
	}

	unittest // example2
	{
		string example =
`9999 1
0`;
		if (example.empty) return;
		Input input = void;
		parseExample(input, example);
		auto result = main2(&input);
		printResult(result);
		assert(result == 9999);
	}

	unittest // example3
	{
		string example =
`1129 1
0 2 9`;
		if (example.empty) return;
		Input input = void;
		parseExample(input, example);
		auto result = main2(&input);
		printResult(result);
		assert(result == 1131);
	}

	unittest { writeln("end unittest"); }

	void parseExample(out Input input, string example)
	{
		struct Adapter
		{
			string[] _lines;
			this(string input) { _lines = input.splitLines(); }
			string readln() { auto line = _lines[0]; _lines = _lines[1..$]; return line; }
		}
		parseInput(input, Adapter(example));
	}
}

void printResult(T)(T result)
{
	static if (isFloatingPoint!T) writefln("%f", result);
	else writeln(result);
}

void main()
{
	Input input = void;
	parseInput(input, stdin);
	alias retType = ReturnType!main2;
	static if (is(retType == void))
		main2(&input);
	else
	{
		auto result = main2(&input);
		printResult(result);
	}
}


Measured memory_bytes: 256