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));
}


Measured memory_bytes: 4352