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>

int main (void)
{
	int n,k,i,t,s,sum=0;
	int a=0,b=0,c=0,d=0,e=0;
	scanf("%d",&n);
	scanf("%d",&k);
	int array0[k],array[10-k],array1[5];
	for(i=0;i<k;i++){
		scanf("%d",&array0[i]);
		}
	for(t=0;t<10-k;){
		for(s=0;s<=9;){
			for(i=0;i<k;i++){
				if(s==array0[i]) {
					s+=1;
					i=0;
				}
			}
		array[t]=s;
		s+=1;
		t+=1;	
		}
	}
	array1[0]=n%10;
	array1[1]=n%100-n%10;
	array1[2]=n%1000-n%100;
	array1[3]=n%10000-n%1000;

	while(1){
		if(array1[0]<=array[e]){
			sum+=array[e];
			break;
			}
		if(e==9-k){
			sum+=array[0];
			array1[1]++;
			break;
			}
		e++;
		}

	if(sum>=n){
		printf("%d\n",sum);
		return 0;
		}
	while(1){					
		if(array1[1]==array[d]*10){
			sum+=array[d]*10;
			break;
			}
		if(array1[1]<array[d]*10){
			sum=0;
			sum+=array[d]*10;
			sum+=array[0];
			break;
			}
		if(d==9-k){
			sum=0;
			sum+=array[0];
			sum+=array[0]*10;
			array1[2]++;
			break;
			}
		d++;
		}

	if(sum>=n){
		printf("%d\n",sum);
		return 0;
		}

	while(1){
		if(array1[2]==array[c]*100){
			sum+=array[c]*100;
			break;			
			}
		if(array1[2]<array[c]*100){
			sum=0;
			sum+=array[c]*100;
			sum+=array[0]*10;
			sum+=array[0];
			break;
					}
		if(c==9-k){
			sum=0;
			sum+=array[0];
			sum+=array[0]*10;
			sum+=array[0]*100;
			array1[3]++;
			break;
			}
		c++;			
		}
	if(sum>=n){
		printf("%d\n",sum);
		return 0;
		}
	while(1){
		if(array1[3]==array[b]*1000){
			sum+=array[b]*1000;
			break;
			}
		if(array1[3]<array[b]*1000){
			sum=0;
			sum+=array[b]*1000;
			sum+=array[0]*100;
			sum+=array[0]*10;
			sum+=array[0];
			break;
					}
		if(c==9-k){
			sum=0;
			sum+=array[0];
			sum+=array[0]*10;
			sum+=array[0]*100;
			sum+=array[0]*1000;
			break;
			}
		b++;		
		}

	if(sum>=n){
		printf("%d\n",sum);
		return 0;
		}
	while(1){
		sum+=array[a]*10000;
		if(sum>=n){
			printf("%d\n",sum);
			break;
		}
		a++;
	}
	return 0;
}