import std.stdio;

void main(){
	auto cin = new Cin();
	//auto j = new Joint();
	
	auto NK = cin.line();
	auto D = cin.line();
	
	auto N = NK[0];
	
	solve( N , D ).writeln();
	
	
}
auto solve( size_t n , size_t[] ng ){
	bool[10] nglist;
	nglist[] = true;
	bool up = false;
	
	foreach( elm ; ng ){
		nglist[elm] = false;
	}
	
	forD:foreach( i ; n..n+100_000 ){
		up = false;
		
		foreach_reverse( j ; 0..5 ){
			n = i%(10^^(j+1))/(10^^j);
			
			if( !up && n==0 ){
				continue;
			}else{
				up = true;
			}
			
			
			if( nglist[n] == false ){
				continue forD;
			}
		}
		return i;
	}
	return 0;
}
unittest{
	assert( solve(1111,[1,3,4,5,6,7,8,9]) == 2000 );
	assert( solve(9999,[0]) == 9999 );
	
	assert( solve(9999,[9]) == 10000 );
	assert( solve(10000,[0,9]) == 11111 );
	assert( solve(10000,[1,0]) == 22222 );
	assert( solve(5555,[5]) == 6000 );
	assert( solve(5959,[9]) == 6000 );
	
}


import std.stdio,std.conv,std.string;
import std.algorithm,std.array;

class Cin
{
	T[] line( T = size_t , string token = " " )( size_t m = 1 ){
		T[] arr = [];
		foreach( i ; 0..m ){
			arr ~= this.read!T();
		}
		return arr;
	}
	T[][] rect( T = real , string token = " " )( size_t m = 1 ){
		T[][] arr = new T[][]();
		foreach( i ; 0..m ){
			arr[i] = this.read!T(token);
		}
		return arr;
	}
	private T[] read( T = size_t )( string token = " " ){
		T[] arr;
		foreach( elm ; readln().chomp().split(token) ){
			arr ~= elm.to!T();
		}
		return arr;
	}
}