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<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int k;
int vis[100010],dis[100010];
int first[100010],cnt;
struct node{
	int to,next,val;
}edge[400010];
void add(int u,int v,int val){
	edge[++cnt].to = v;
	edge[cnt].val = val;
	edge[cnt].next = first[u];
	first[u] = cnt;
}
struct N{
	int x,dis;
	
};
bool operator < (N a, N b){
		return a.dis > b.dis;
}
priority_queue<N>q;
void dij(){
	memset(dis,0x3f,sizeof dis);
	dis[1] = 0;
	q.push((N){1,1});
	while(q.size()){
		N cur = q.top();q.pop();
		int x = cur.x;
		int ds = cur.dis;
		if(vis[x])continue;
		vis[x] = 1;
		for(int i = first[x];i;i = edge[i].next){
			int to = edge[i].to;
			int val = edge[i].val;
			if(dis[to] > ds + val){
				dis[to] = ds + val;
				q.push((N){to,dis[to]});
			}
		}
	}
}

int main(){
	cin >> k;
	for(int i = 0; i < k; i++){
		add(i%k,(i+1)%k,1);
		add(i%k,(i*10)%k,0);
	}
	dij();
	cout << dis[0];
}