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: Accepted)

use std::str::FromStr;
use std::io::*;
use utils::*;

pub fn main() {
    let i = stdin();
    let mut o = Vec::new();
    run(i.lock(), &mut o);
    stdout().write_all(&o).unwrap();
}

fn run<R: BufRead, W: Write>(i: R, o: &mut W) {
    let mut i = AtRead::from(i);
    let (n, k) = i.read2::<usize, usize>();
    let d = i.read_vec::<usize>(k);
    writeln!(o, "{}", solve(n, &d)).unwrap();
}
fn solve(mut n: usize, d: &[usize]) -> usize {
    let mut t = [true; 10];
    for &d in d {
        t[d] = false;
    }

    let mut uppers = [None; 10];
    let mut min = None;
    for i in 0..10 {
        let i = 9 - i;
        if t[i] {
            min = Some(i);
        }
        uppers[i] = min;
    }

    let mut rs = Vec::new();
    while n != 0 {
        rs.push(n % 10);
        n /= 10;
    }
    if rs.is_empty() {
        rs.push(0);
    }

    let mut i = 0;
    let mut i_up = 0;
    let mut is_up = false;
    loop {
        if i == rs.len() {
            if is_up {
                rs.push(0);
            } else {
                break;
            }
        }
        let mut r = rs[i];
        if is_up && r < 9 {
            is_up = false;
            r = r + 1;
            i_up = i;
        }
        if !is_up & !t[r] {
            if let Some(u) = uppers[r] {
                i_up = i;
                r = u;
            } else {
                is_up = true;
            }
        }
        rs[i] = r;
        i += 1;
    }
    for i in 0..i_up {
        rs[i] = min.unwrap();
    }
    let mut result = 0;
    rs.reverse();
    for &r in &rs {
        result *= 10;
        result += r;
    }
    result
}

mod utils {
    #![allow(dead_code)]
    use super::*;

    pub struct AtRead<R: BufRead> {
        r: R,
        s: String,
    }

    macro_rules! fn_read {
        {$f:ident($($v:ident: $t:ident),*)} => {
            pub fn $f<$($t: FromStr),*>(&mut self) -> ($($t),*) {
                let i = &mut self.read_line().split(' ');
                $(
                    let $v = next(i);
                )*
                ($($v),*)
            }
        };
    }

    impl<R: BufRead> AtRead<R> {
        pub fn from(r: R) -> Self {
            AtRead {
                r: r,
                s: String::new(),
            }
        }
        pub fn read_line(&mut self) -> &str {
            self.s.clear();
            self.r.read_line(&mut self.s).unwrap();
            self.s.trim()
        }

        pub fn read<T: FromStr>(&mut self) -> T {
            self.read_line().parse().ok().unwrap()
        }
        fn_read! { read2(v1: T1, v2: T2) }
        fn_read! { read3(v1: T1, v2: T2, v3: T3) }
        fn_read! { read4(v1: T1, v2: T2, v3: T3, v4: T4) }

        pub fn read_vec<T: FromStr>(&mut self, n: usize) -> Vec<T> {
            self.read_line()
                .split(' ')
                .take(n)
                .map(|x| x.parse().ok().unwrap())
                .collect()
        }
    }

    fn next<'a, T: FromStr, I: std::iter::Iterator<Item = &'a str>>(i: &mut I) -> T {
        i.next().unwrap().parse().ok().unwrap()
    }
}