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 Rust (1.15.1) (judge verdict: Wrong Answer)

/// Rustで競技プログラミングの入力をスッキリ記述するマクロ
///
/// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

#[inline]
#[allow(dead_code)]
fn gcd(mut m: u64, mut n: u64) -> u64 {
    if m < n {
        std::mem::swap(&mut m, &mut n);
    }

    loop {
        let k = m % n;
        if k == 0 {
            return n;
        }

        m = n;
        n = k;
    }
}

#[inline]
#[allow(dead_code)]
fn lcm(m: u64, n: u64) -> u64 {
    m / gcd(m, n) * n
}

// nc と同じ値を取れない最上位の桁を返す
fn not_equal_digit(nc: &[char], ok_vc: &[char]) -> Option<usize> {
    for (digit, c) in nc.iter().rev().enumerate().rev() {
        if !ok_vc.contains(c) {
            return Some(digit);
        }
    }
    None
}

// digit 以上の桁のうち ni より大きな値が取れる最下位の桁と、その値のタプルを返す
fn over_digit(ni: &[i64], ok_vi: &[i64], digit: usize) -> (usize, i64) {
    for (digit, &i) in ni.iter().rev().enumerate().skip(digit) {
        for &ok_i in ok_vi {
            if ok_i > i {
                return (digit, ok_i);
            }
        }
    }

    (ni.len(), *ok_vi.iter().find(|&&ok_i| ok_i > 0).unwrap())
}

fn main() {
    input!{
        nc: chars,
        k: usize,
        d_v: [i64; k],
    }

    let ni: Vec<i64> = nc.iter().map(|c| c.to_digit(10).unwrap() as i64).collect();

    let ok_vi: Vec<i64> = (0..10i64)
        .into_iter()
        .filter(|i| !d_v.contains(i))
        .collect();

    let ok_vc: Vec<char> = ok_vi
        .iter()
        .map(|&i| std::char::from_digit(i as u32, 10).unwrap())
        .collect();

    println!("ok_vc = {:?}", ok_vc);

    if let Some(not_equal_digit) = not_equal_digit(&nc, &ok_vc) {
        let (over_digit, over_num) = over_digit(&ni, &ok_vi, not_equal_digit);

        for digit in (0..(ni.len()+1)).rev() {
            if digit == over_digit {
                print!("{}", over_num);
            } else if digit > over_digit && digit >= ni.len() {
                continue;
            } else if digit > over_digit {
                print!("{}", nc[digit]);
            } else {
                print!("{}", ok_vc[0]);
            }
        }
        println!("");
        return;
    } else {
        for c in &nc {
            print!("{}", c);
        }
        println!("");
        return;
    }
}