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::collections::BinaryHeap;

macro_rules! scan {
    ($t:ty) => {
        {
            let mut line: String = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            line.trim().parse::<$t>().unwrap()
        }
    };
    ($t:ty; $n:expr) => {
        (0..$n).map(|_|
                    scan!($t)
        ).collect::<Vec<_>>()
    };
    ($t:ty;;$n:expr) => {
        (0..$n).map(|_| scan!($t;;)).collect::<Vec<_>>()
    };

    ($t:ty;;) => {
        {
            let mut line: String = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            line.split_whitespace()
                .map(|t| t.parse::<$t>().unwrap())
                .collect::<Vec<_>>()
        }
    };
    ($($t:ty),*; $n:expr) => {
        (0..$n).map(|_|
                    scan!($($t),*)
        ).collect::<Vec<_>>()
    };

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

fn dikstra(graph: Vec<Vec<(usize, i64)>>) -> i64 {
    let n = graph.len();
    let mut dists: Vec<_> = (0..n).map(|_| (1i64 << 60)).collect();

    let mut q = BinaryHeap::new();
    q.push((0, 1));
    while let Some((dist, v)) = q.pop() {
        for &(to, d) in graph[v].iter() {
            if -dist + d < dists[to] {
                dists[to] = -dist + d;
                q.push((dist - d, to))
            }
        }
    }
    dists[0]
}

#[test]
fn test_tuple_binheap() {
    let mut q = BinaryHeap::new();
    q.push((-3, 2));
    q.push((-2, 3));
    q.push((-1, 5));
    assert_eq!(q.pop(), Some((-1, 5)));
    assert_eq!(q.pop(), Some((-2, 3)));
    assert_eq!(q.pop(), Some((-3, 2)));
}

fn main() {
    let k = scan!(i64);

    let mut graph = Vec::new();
    for _ in 0..k {
        graph.push(Vec::new())
    }

    for i in 1..k {
        graph[i as usize].push((((i + 1) % k) as usize, 1));
        graph[i as usize].push((((10 * i) % k) as usize, 0));
    }
    println!("{}", dikstra(graph) + 1)
}