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];
	bool[10] d;
	d[0..$] = true;
	foreach( elm ; D ){
		d[elm] = false;
	}
	size_t[] ans;
	bool up;
	
	size_t n;
	
	forD:foreach( i ; 0..4 ){
		n = N/(10^^i)%(10^^i);
		foreach( j ; n..10 ){
			if( j==n && up ){
				up=false;continue;
			}
			if( d[j] == true ){
				ans = j ~ ans;
				continue forD;
			}
		}
		foreach( j ; 0..n ){
			if( d[j] == true ){
				ans = j ~ ans;
				up = true;
				continue forD;
			}
		}
	}
	
	foreach( elm ; ans ){
		elm.write();
	}
	writeln("");
	//writeln( cin.line!size_t(2).sum() , " " , cin.line!string()[0] );
}


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;
	}
}