type Tmas = array[1..100000] of shortint;
type Tymas = array[1..100000] of longint;
function hasdigit(n:longint;z:shortint):boolean;
var res:boolean;
begin
  res := false;
  while (n <> 0) and (not(res)) do
  begin
    if (n mod 10) = z then res := true;
    n := n div 10;
  end;
  hasdigit := res;
end;
var nums,a:Tmas;error,i,j,counter,digitquantity,k:longint;S,N:ansistring;b:Tymas;
begin
  readln(S);
  i:=1;
  j:=1;
  while i<=length(S) do
  begin
    val(S[i],counter,error);
    nums[j] := counter;
    i := i + 2;
    inc(j);
  end;
  digitquantity:=j-1;
  read(k);
  fillchar(a,length(a),1);
  for i:=1 to 100000 do
    for j:=1 to digitquantity do
      if hasdigit(i,nums[j]) then a[i] := -1;
  j := 1;
  for i:=1 to 100000 do
  begin
    if a[i] <> -1 then
    begin
      b[j]:=i;
      inc(j);
    end;
  end;
  i := 1;
  while b[i]<>k do
    inc(i);
  writeln(i);
end.