function main() {
  process.stdin.resume();
  process.stdin.setEncoding("utf8");
  var chunks = "";

  process.stdin.on("data", function (chunk) {
    chunks += chunk;
  });

  process.stdin.on("end", function () {
    var inputs = chunks.trim().split("\n");
    var number = inputs[0].split(" ")[0].split("");
    var ks = inputs[1].split(" ").map((k) => parseInt(k, 10));
    var ans = [];

    var carry = false;
    for (i = number.length - 1; i >= 0; i--) {
      var n = parseInt(number[i], 10);
      if (carry) {
        n = n + 1;
        if (n >= 10) {
          n = 10 - n;
          carry = true;
        } else {
          carry = false;
        }
      }

      while (true) {
        if (ks.find((k) => k === n)) {
          n = n + 1;
          if (n >= 10) {
            n = 10 - n;
            carry = true;
          }
        } else {
          ans.unshift(n);
          break;
        }
      }
    }

    if (carry) {
      ans.unshift(1);
    }

    console.log(parseInt(ans.join(""), 10));
  });
}

main();
