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 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <stdio.h>
#include <stdbool.h>
#include <string.h>

void solve_042();
void gbnStrPaddingRight(char *str, int buf_length, char padding_char); // buf_length桁になるようstrを右詰めにする

#define MAX_COLUMN 4

int N[MAX_COLUMN + 1]; // 正規価格, 各桁ごとに保存。制約：N<10000, 最上位がNG番号でキャリーが発生した時のために5桁まで確保
int K; // 嫌いな数値の数
bool dislike[10]; //嫌いな数フラグ
char not_dislike_minimum; // 一番小さい使用可能数
char replace_number[10]; // NG番号だった場合に置き換える数値
char isCarry[10]; // NG番号を置き換えた場合にキャリーが発生するか

void solve_042()
{
 	int i;

 	int firstNG = -1;
	for (i = 1; i <= MAX_COLUMN; ++i){
		if (dislike[N[i]]){	// 上の桁から探して最初のNGナンバー
			firstNG = i;
			break;
		}
	}

	for (++i; i <= MAX_COLUMN; ++i){
		if (dislike[N[i]]) // NGナンバーなら
			N[i] = not_dislike_minimum;   // 一番小さい使用可能数に変える
	}

	bool isClear = false;
	while(1){ // firstNG桁より上の桁の処理、carryが無くなるまで繰り返す
		for (i = N[firstNG]; i <= 9; i++){
			if (!dislike[i]){ // firstNG桁の数字～9までの間にOKな数字があれば
				N[firstNG] = i;
				isClear = true;
				break;
			}
		}
		if (isClear) break;

		N[firstNG] = not_dislike_minimum; // firstNG桁の数字～9までの間にOKな数字が無い場合
		N[--firstNG]++; // firstNGを１桁上げて１を足し
		if(!dislike[N[firstNG]]) break; // NG数でなければwhileから出る
	}

	int ans = N[0] * 10000 + N[1] * 1000 + N[2] * 100 + N[3] * 10 + N[4];
	printf("%d\n", ans);
}

void gbnStrPaddingRight(char *str, int buf_length, char padding_char)
// buf_length桁になるようstrを右詰めにする
// 2019-09-26 作成
//
{
	int str_length = (int)strlen(str);
	int length_short = buf_length - str_length; // 桁数の不足分
	if (length_short <= 0) return;

	int ptr;
	for (ptr = str_length; ptr >= 0; --ptr) // 桁数不足分、元のデータを右にずらす
		str[ptr + length_short] = str[ptr];
	for (ptr = 0; ptr < length_short; ++ptr) // 空いた前方を指定文字で埋める
		str[ptr] = padding_char;
}

int main()
{
	int i;
	for (i = 0; i < 10; i++) dislike[i] = false;

	char inStr[5]; // 制約：N < 10000
	int inNum;
	scanf("%s %d", inStr, &K);
	for (i = 0; i < K; i++){
		scanf("%d", &inNum);
		dislike[inNum] = true;
	}

	for (i = 9; i >= 0; --i)
		if (!dislike[i]) not_dislike_minimum = i;

	gbnStrPaddingRight(inStr, 4, '0');
	N[0] = 0;
	for (i = 0; i < 4; ++i) N[i + 1] = (int)(inStr[i] - '0');

	solve_042();
	return 0;
}