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: Wrong Answer)

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define MP make_pair
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define PLI pair<ll, int>
#define PIL pair<int, ll>
#define STR string
#define Fst first
#define Snd second
#define SHN 1e9
#define LSHN 1e18
using namespace std;
bool visit[100010];
deque<int> dq;
int n;
int main()
{
	cin >>n;
	dq.push_back(1),dq.push_back(1);
	while (1)
	{
		int x=dq.front(); dq.pop_front();
		int y=dq.front(); dq.pop_front();
		if (!x)
		{
			cout <<y;
			return 0;
		}
		if (!visit[(x*10)%n])
		{
			visit[(x*10)%n]=1;
			dq.push_front(y);
			dq.push_front((x*10)%n);
		}
		if (!visit[(x+1)%n])
		{
			visit[(x+1)%n]=1;
			dq.push_back((x+1)%n);
			dq.push_back(y+1);
		}
	}
	return 0;
}