Problem:
Score : 700 points

Problem Statement

Find the smallest possible sum of the digits in the decimal notation of a positive multiple of K.

Constraints

2 \leq K \leq 10^5

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print the smallest possible sum of the digits in the decimal notation of a positive multiple of K.

Sample Input 1

6

Sample Output 1

3

12=6×2 yields the smallest sum.

Sample Input 2

41

Sample Output 2

5

11111=41×271 yields the smallest sum.

Sample Input 3

79992

Sample Output 3

36

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

#[allow(unused_macros)]
macro_rules! parse_line {
    ( $t:ty ) => (
        {
            let mut line = String::new();
            ::std::io::stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            iter.next().unwrap().parse::<$t>().unwrap()
        }
    );

    ( $( $t:ty), +) => (
        {
            let mut line = String::new();
            ::std::io::stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            ( $(iter.next().unwrap().parse::<$t>().unwrap()),* )
        }
    );
}

#[allow(unused_macros)]
macro_rules! read_line {
    () => (
        {
            let mut line = String::new();
            ::std::io::stdin().read_line(&mut line).unwrap();
            line.pop();
            line
        }
    )
}

#[allow(unused_macros)]
macro_rules! parse_vec {
    ( $t:ty ) => (
        {
            let mut line = String::new();
            ::std::io::stdin().read_line(&mut line).unwrap();
            let iter = line.split_whitespace();
            iter.map(|v| v.parse::<$t>().unwrap()).collect::<Vec<_>>()
        }
    )
}

fn solve(k: u64) -> u64 {
    use std::collections::VecDeque;
    use std::rc::Rc;

    let mut deque = Rc::new(VecDeque::new());
    let mut memo = vec![false; k as usize];

    Rc::make_mut(&mut deque).push_front((1, 1));

    loop {
        let mut nd = Rc::new(VecDeque::new());

        for &(i,j) in deque.iter() {
            {
                let n = (i*10) % k;
                if n == 0 {
                    return j
                }

                if !memo[n as usize] {
                    memo[n as usize] = true;
                    Rc::make_mut(&mut nd).push_front((n, j));
                }
            }

            {
                let n = (i+1) % k;
                if n == 0 {
                    return j+1
                }

                if !memo[n as usize] {
                    memo[n as usize] = true;
                    Rc::make_mut(&mut nd).push_back((n, j+1));
                }
            }
        }

        deque = nd;
    }

    unreachable!();
}

fn main() {
    let k = parse_line!(u64);
    println!("{}", solve(k));
}

#[test]
fn solve_test() {
    assert_eq!(solve(6), 3);
    assert_eq!(solve(41), 5);
    assert_eq!(solve(4), 1);
    assert_eq!(solve(79992), 36);
}