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>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii; 

const int maxn = 1e5 + 10;
int dist[maxn]; 
int main(int argc, char const *argv[]) {
	int k; cin >> k; 
	deque<int> q;
	for(int i = 0; i <= k; i++) 
		dist[i] = 1e9; 

	dist[1] = 1;
	q.push_back(1);

	while(!q.empty()) {
		int u = q.front(); q.pop_front(); 
		if(u == 0) { cout << dist[0] << endl; return 0; }
		int v = (u + 1) % k;
		if(dist[v] > dist[u] + 1) {
			dist[v] = dist[u] + 1; 
			q.push_back(v); 
		}
		v = (u * 10) % k;
		if(dist[v] > dist[u]) {
			dist[v] = dist[u];
			q.push_front(v);
		}
	}
}