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.*;

public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int k = sc.nextInt();
    int[] dist = new int[k];
    for(int i = 0; i < k; i++) {
      dist[i] = Integer.MAX_VALUE;
    }
    dist[1] = 0;
    Deque<Integer> deque = new ArrayDeque<>();
    deque.addFirst(1);
    int[] visit = new int[k];
    visit[1] = 1;
    while(deque.size() > 0) {
      int u = deque.pollFirst();
      int v1 = (10 * u) % k;
      int v2 = (u + 1) % k;
      if(visit[v1] == 0)  {
        dist[v1] = Math.min(dist[v1], dist[u]);
        deque.addFirst(v1);
        visit[v1] = 1;
      } else {
        if(dist[v1] > dist[u]) {
          dist[v1] = dist[u];
          deque.addFirst(v1);
        }
      }
      if(visit[v2] == 0) {
        dist[v2] = Math.min(dist[v2], dist[u] + 1);
        deque.addLast(v2);
        visit[v2] = 1;
      }
    }
    System.out.println(dist[0] + 1);
  }
}