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::io;

struct IrohaChan {
    digits: Vec<bool>,
}

impl IrohaChan {
    fn new(digits: Vec<usize>) -> Self {
        let mut available_digits = vec![true; 10];
        for i in digits {
            available_digits[i] = false;
        }
        IrohaChan{
            digits: available_digits,
        }
    }

    fn likes(&self, n: usize) -> bool {
        let mut k = n;
        while k > 0 {
            let d = k % 10;
            k /= 10;
            if !self.digits[d] {
                return false
            }
        }
        return true
    }

    fn purchase(&self, min_price: usize) -> usize {
        let mut i = min_price;
        loop {
            if self.likes(i) {
                return i
            }
            i += 1;
        }
    }
}

#[test]
fn test_iroha_likes_with_empty() {
    let iroha = IrohaChan::new(vec![]);
    assert!(iroha.likes(0));
    assert!(iroha.likes(1));
    assert!(iroha.likes(2));
    assert!(iroha.likes(3));
    assert!(iroha.likes(4));
    assert!(iroha.likes(5));
    assert!(iroha.likes(6));
    assert!(iroha.likes(7));
    assert!(iroha.likes(8));
    assert!(iroha.likes(9));

    assert!(iroha.likes(123));
    assert!(iroha.likes(456));
    assert!(iroha.likes(789));

    assert!(iroha.likes(12345));
    assert!(iroha.likes(67890));
}

#[test]
fn test_iroha_likes_with_singleton() {
    let iroha = IrohaChan::new(vec![3]);
    assert!(iroha.likes(0));
    assert!(iroha.likes(1));
    assert!(iroha.likes(2));
    assert!(!iroha.likes(3));
    assert!(iroha.likes(4));
    assert!(iroha.likes(5));
    assert!(iroha.likes(6));
    assert!(iroha.likes(7));
    assert!(iroha.likes(8));
    assert!(iroha.likes(9));

    assert!(!iroha.likes(123));
    assert!(iroha.likes(456));
    assert!(iroha.likes(789));

    assert!(!iroha.likes(12345));
    assert!(iroha.likes(67890));
}

#[test]
fn test_iroha_likes_with_multiple_digits() {
    let iroha = IrohaChan::new(vec![2, 8]);
    assert!(iroha.likes(0));
    assert!(iroha.likes(1));
    assert!(!iroha.likes(2));
    assert!(iroha.likes(3));
    assert!(iroha.likes(4));
    assert!(iroha.likes(5));
    assert!(iroha.likes(6));
    assert!(iroha.likes(7));
    assert!(!iroha.likes(8));
    assert!(iroha.likes(9));

    assert!(!iroha.likes(123));
    assert!(iroha.likes(456));
    assert!(!iroha.likes(789));

    assert!(!iroha.likes(12345));
    assert!(!iroha.likes(67890));

    assert!(!iroha.likes(28));
    assert!(iroha.likes(39));
}

#[test]
fn test_iroha_purchase_1() {
    let iroha = IrohaChan::new(vec![1, 3, 4, 5, 6, 7, 8, 9]);
    assert_eq!(iroha.purchase(1000), 2000)
}

#[test]
fn test_iroha_purchase_2() {
    let iroha = IrohaChan::new(vec![0]);
    assert_eq!(iroha.purchase(9999), 9999)
}

fn main() {
    let mut input = String::new();
    io::stdin().read_line(&mut input).unwrap();
    let mut iter = input.split_whitespace();

    let n: usize = iter.next().unwrap().parse().unwrap();
    let k: usize = iter.next().unwrap().parse().unwrap();

    let mut digits = Vec::with_capacity(k);
    let mut line = String::with_capacity(2 * k);
    io::stdin().read_line(&mut line).unwrap();
    for token in line.split_whitespace() {
        digits.push(token.parse().unwrap());
    }

    let iroha = IrohaChan::new(digits);

    println!("{}", iroha.purchase(n));
}