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 C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
class Program
{
	static void Main(string[] args)
	{
		string str = Console.ReadLine();
		int K = int.Parse(str);
		int[]N = new int[K];
		var pq = new PriorityQueue<int>((x,y)=>N[x]-N[y]);
		pq.Enqueue(1);
		N[1] = 1;
		while(true){
			int ni = pq.Dequeue();
			if(N[ni] <= N[0] && N[0] > 0){
				break;
			}
			if(N[ni] + 1 < N[(ni+1)%K] || N[(ni+1)%K] == 0){
				N[(ni+1)%K] = N[ni] + 1;
				pq.Enqueue((ni+1)%K);
			}
			if(N[ni] < N[(ni*10)%K] || N[(ni*10)%K] == 0){
				N[(ni*10)%K] = N[ni];
				pq.Enqueue((ni*10)%K);
			}
		}
		Console.WriteLine(N[0]);
	}
	
    public class PriorityQueue<T>
    {
        readonly List<T> heap = new List<T>();
        readonly Comparison<T> cmp;
        public PriorityQueue() { cmp = Comparer<T>.Default.Compare; }
        public PriorityQueue(Comparison<T> comparison) { cmp = comparison; }
        public PriorityQueue(IComparer<T> comparer) { cmp = comparer.Compare; }
        public void Enqueue(T item)
        {
            var pos = heap.Count;
            heap.Add(item);
            while (pos > 0)
            {
                var par = (pos - 1) / 2;
                if (cmp(heap[par], item) <= 0)
                    break;
                heap[pos] = heap[par];
                pos = par;
            }
            heap[pos] = item;

        }
        public T Dequeue()
        {
            var ret = heap[0];
            var pos = 0;
            var x = heap[heap.Count - 1];

            while (pos * 2 + 1 < heap.Count - 1)
            {
                var lch = pos * 2 + 1;
                var rch = pos * 2 + 2;
                if (rch < heap.Count - 1 && cmp(heap[rch], heap[lch]) < 0) lch = rch;
                if (cmp(heap[lch], x) >= 0)
                    break;
                heap[pos] = heap[lch];
                pos = lch;
            }
            heap[pos] = x;
            heap.RemoveAt(heap.Count - 1);
            return ret;

        }
        public T Peek() { return heap[0]; }
        public int Count { get { return heap.Count; } }
        public bool Any() { return heap.Count > 0; }
        public T[] Items
        {
            get
            {
                var ret = heap.ToArray();
                Array.Sort(ret, cmp);
                return ret;
            }
        }
    }

}