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.42.0) (judge verdict: Accepted)

use proconio::input;
// dbg {{{
#[allow(dead_code)]
mod dbg {
    use std::fmt::{Debug, Formatter};

    #[derive(Clone)]
    pub struct Tabular<'a, T: Debug>(pub &'a [Vec<T>]);

    impl<'a, T: Debug> Debug for Tabular<'a, T> {
        fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
            for i in 0..self.0.len() {
                writeln!(f, "{:2} | {:?}", i, &self.0[i])?;
            }
            Ok(())
        }
    }
}
// }}}
use dbg::Tabular;
use std::cmp::Reverse;

fn main() {
    input!(k: usize);
    let mut g = vec![vec![]; k];
    for i in 0..k {
        for j in 0..10 {
            g[i].push(((10 * i + j) % k, j as u32));
        }
    }

    let mut dist = vec![std::u32::MAX; k];
    let mut heap = std::collections::BinaryHeap::new();
    for i in 1..k {
        dist[i] = i as u32;
        heap.push((Reverse(i as u32), i));
    }
    while let Some((Reverse(d), x)) = heap.pop() {
        if dist[x] < d {
            continue;
        }
        for &(y, w) in &g[x] {
            let dy = dist[x] + w;
            if dy < dist[y] {
                heap.push((Reverse(dy), y));
                dist[y] = dy;
            }
        }
    }
    println!("{}", dist[0]);
}