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 summin = 9999;
		if(K % 9 == 0){
			int sum = 0;
			sum += K / 100000;
			sum += (K / 10000) % 10;
			sum += (K / 1000) % 10;
			sum += (K / 100) % 10;
			sum += (K / 10) % 10;
			sum += K % 10;
			summin = sum;
		} else {
			int i = 1;
			if(K == 1 || K%10 == 0 || K%100 == 0 || K%1000 == 0 || K%10000 == 0 || K%100000 == 0){
				summin = 1;
			} else {
				while(i<100000){
					int a = K * i;
					int sum = 0;
					sum += a / 100000;
					sum += (a / 10000) % 10;
					sum += (a / 1000) % 10;
					sum += (a / 100) % 10;
					sum += (a / 10) % 10;
					sum += a % 10;
					summin = Math.Min(summin,sum);
					i += 1;
				}
			}
		}
		Console.WriteLine(summin);
	}
}