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<bits/stdc++.h>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;
#define MAXN 100000+10
const int INF=99999999;
struct Ed{
	int u,dis;
	Ed(){}
	Ed(int u,int dis):u(u),dis(dis){}
	bool operator >(const Ed &a)const{return dis>a.dis;}
};
typedef __gnu_pbds::priority_queue<Ed,greater<Ed>,__gnu_pbds::thin_heap_tag> heap;
heap q;
heap::point_iterator it[MAXN];
int k,vis[MAXN],dis[MAXN];
void dijkstra(){
	for(int i=0;i<k;i++)dis[i]=INF;	
	dis[1]=0;
	for(int i=0;i<k;i++)it[i]=q.push((Ed){i,dis[i]});
	while(!q.empty()){
		int t,u=q.top().u;
		q.pop();
		if(vis[u])continue;
		vis[u]=1;
		t=u*10%k;
		if(!vis[t]&&dis[u]<dis[t]){
			dis[t]=dis[u];
			q.modify(it[t],(Ed){t,dis[t]});
		}
		t=(u+1)%k;
		if(!vis[t]&&dis[u]+1<dis[t]){
			dis[t]=dis[u]+1;
			q.modify(it[t],(Ed){t,dis[t]});
		}
	}
}
int main(){
	scanf("%d",&k);
	dijkstra();
	printf("%d\n",dis[0]+1);
	return 0;
}