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

use std::cmp::Ordering;
use std::collections::BinaryHeap;
use std::io;

#[allow(unused_macros)]
macro_rules! input {
    ( $($x:expr ),*) => {
        {
            let temp_str = input_line_str();
            let mut split_result_iter = temp_str.split_whitespace();
                $(
                let buf_split_result = split_result_iter.next();
                let buf_split_result = buf_split_result.unwrap();
                    ($x) = buf_split_result.parse().unwrap();
                )*
        }
    };
}

#[allow(dead_code)]
fn input_line_str() -> String {
    let mut s = String::new();
    io::stdin().read_line(&mut s).unwrap();
    s.trim().to_string()
}

#[allow(dead_code)]
fn p<T>(t: T)
where
    T: std::fmt::Display,
{
    println!("{}", t);
}

#[derive(Eq)]
struct St {
    cost: usize,
    edge: usize,
}

impl Ord for St {
    fn cmp(&self, other: &St) -> Ordering {
        other.cost.cmp(&self.cost)
    }
}
impl PartialOrd for St {
    fn partial_cmp(&self, other: &St) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}
impl PartialEq for St {
    fn eq(&self, other: &Self) -> bool {
        self.cost == other.cost
    }
}

fn main() {
    let k: usize;
    input!(k);
    let mut v: Vec<usize> = vec![999_999_999; k];
    let mut hp = BinaryHeap::new();
    v[1] = 1;
    hp.push(St {
        cost: 0,
        edge: 10 % k,
    });
    hp.push(St {
        cost: 1,
        edge: 2 % k,
    });

    while !hp.is_empty() {
        let e = hp.pop().unwrap();
        if v[e.edge] > e.cost {
            v[e.edge] = e.cost;
            hp.push(St {
                cost: e.cost + 1,
                edge: (e.edge + 1) % k,
            });
            hp.push(St {
                cost: e.cost,
                edge: (e.edge * 10) % k,
            });
        }
    }
    p(v[0] + 1);
}