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: Wrong Answer)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.Collection;
import java.io.IOException;
import java.io.BufferedReader;
import java.util.Queue;
import java.util.LinkedList;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskD solver = new TaskD();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskD {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int K = in.nextInt();
            out.println(sumDigits(find2(K)));
        }

        private int sumDigits(String s) {
            int ans = 0;
            for (int i = 0; i < s.length(); i++) {
                ans += (s.charAt(i) - '0');
            }
            return ans;
        }

        private String find2(int K) {
            Queue<TaskD.Num> queue = new LinkedList<>();
            queue.add(new TaskD.Num("1", 1 % K));
            boolean rems[] = new boolean[K];
            rems[1 % K] = true;

            while (!queue.isEmpty()) {
                TaskD.Num num = queue.poll();
                if (num.remainder == 0) return num.repr;

                for (int d = 0; d < 2; d++) {
                    int newRem = (10 * num.remainder + d) % K;

                    if (!rems[newRem]) {
                        rems[newRem] = true;
                        queue.add(new TaskD.Num(num.repr + d, newRem));
                    }

                }
            }
            return "BRAK";
        }

        private static class Num {
            private String repr;
            private int remainder;

            public Num(String repr, int remainder) {
                this.repr = repr;
                this.remainder = remainder;
            }

        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}