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 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <stdio.h>
#include <math.h>

short tmpval = 0;

void count(int i){
	tmpval = 0;
	while (i > 0){
		tmpval += i % 10;
		i /= 10;
	}
}

int main (void){
	long K;
	short dig = 0;
	short t = 1;
	long T;
	short i;
	short ans;
	long maxval;

	scanf("%ld",&K);
	T = K;

	/*Kの桁数を求め、digに格納*/
	while(t <= K){ 
		t *= 10;
		dig += 1;
	}

	/*探索の上限は10^(dig+1)*/
	maxval = pow(10,dig+1);
	printf("maxval==>%ld\n",maxval);

	i = 2;
	count(K);
	ans = tmpval;
	while(T < maxval){
		printf("tmpval==>%d\n",tmpval);
		printf("T==>%ld\n",T);

		T = K * i;
		count(T);
		if (tmpval < ans){
			ans = tmpval;
		}
		if (ans == 1){
			break;
		}
		i += 1;
	}
	printf("%d\n",ans);
}