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 Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        sc.close();
        Node[] nodes=new Node[k];
        for(int i=0;i<k;nodes[i]=new Node(i++));
        for(int i=1;i<k;i++){
            nodes[i].edges.add(new Edge((i*10)%k, 0));
            if((i*10)%k!=(i+1)%k) nodes[i].edges.add(new Edge((i+1)%k, 1));
        }
        System.out.println(_01bfs(nodes, 1)[0]+1);
    }

    public static int[] _01bfs(Node[] nodes, int s) {
        ArrayDeque<Integer> dq = new ArrayDeque<>();
        dq.add(s);
        int n = nodes.length;
        int[] d = new int[n];
        for (int i = 0; i < n; d[i++] = Integer.MAX_VALUE) ;
        d[s] = 0;
        while (dq.size() > 0) {
            int now = dq.pollFirst();
            for (Edge e : nodes[now].edges) {
                int next = e.id;
                int cost = e.cost;
                if (d[next] > d[now]) {
                    if (cost == 0) {
                        dq.addFirst(next);
                    } else {
                        dq.addLast(next);
                    }
                    d[next] = d[now] + cost;
                }
            }
        }
        return d;
    }

    static class Node {
        int id;
        int value;
        ArrayList<Edge> edges;

        public Node(int id) {
            this.id = id;
            this.value = Integer.MAX_VALUE;
            edges = new ArrayList<Edge>();
        }

        @Override
        public boolean equals(Object o) {
            if (!(o instanceof Node))
                return false;
            Node n = (Node) o;
            if (n.id == id)
                return true;
            else
                return false;
        }

        @Override
        public int hashCode() {
            return id;
        }
    }

    static class Edge {
        int id;
        int cost;
        public Edge(int id, int cost) {
            this.id = id;
            this.cost = cost;
        }
    }
}