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 9.2.1) (judge verdict: Accepted)

#include<stdio.h>
int main(void){
	int k;
	int dp[200100]={0};
	int deque[200200],s,l;
	int cost[200200];
	int look,see;
	scanf("%d",&k);
	deque[0]=1;l=1;
	cost[0]=1;
	while(l){
		look=deque[s];
		see=cost[s];
		s=(s+1)%200000;l--;
		if(dp[look]==0||dp[look]>see){
			dp[look]=see;
			if(dp[(look+1)%k]==0||dp[(look+1)%k]>see+1){
				deque[(s+l)%200000]=(look+1)%k;
				cost[(s+l)%200000]=see+1;
				l++;
			}
			if(dp[look*10%k]==0||dp[look*10%k]>see){
				s=(s+199999)%200000;
				l++;
				deque[s]=look*10%k;
				cost[s]=see;
			}
		}
	}
	printf("%d\n",dp[0]);
	return 0;
}