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++14 (GCC 5.4.1) (judge verdict: Accepted)

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxk=100010;
int k,dl[maxk<<4],f[maxk],id;
bool vis[maxk];
struct edge
{
	int t,w;
	edge *next;
}*con[maxk];
void ins(int x,int y,int w)
{
	edge *p=new edge;
	p->t=y;
	p->w=w;
	p->next=con[x];
	con[x]=p;
}
void spfa()
{
	int head=1,tail=1;
	memset(f,0x3f,sizeof(f));
	dl[1]=1;vis[1]=1;f[1]=0;
	for(int v=1;head<=tail;vis[v]=0,v=dl[++head])
		for(edge *p=con[v];p;p=p->next)
			if(f[p->t]>f[v]+p->w)
			{
				//cout<<v<<' '<<p->t<<' '<<f[v]<<endl;
				f[p->t]=f[v]+p->w;
				if(!vis[p->t]) vis[p->t]=1,dl[++tail]=p->t;
			}
}

int main()
{
	scanf("%d",&k);
	for(int i=1;i<k;i++)
		ins(i,i*10%k,0),ins(i,(i+1)%k,1);
	spfa();	
	printf("%d",f[0]+1);
}