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: Time Limit Exceeded)

// 高速になるかのチェック
// 基本テンプレート

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
using namespace std;

#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define int long long int

template<typename T> void chmax(T &a, T b) { a = max(a, b); }
template<typename T> void chmin(T &a, T b) { a = min(a, b); }
template<typename T> void chadd(T &a, T b) { a = a + b; }

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

int dx[] = { 0, 0, 1, -1 };
int dy[] = { 1, -1, 0, 0 };
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

int dp[50][100001];

struct Elem {
	int sum, mod, val;
	bool operator<(const Elem &a) const {
		return dp[sum][mod] > dp[a.sum][a.mod];
	}
};

signed main() {
	int K; cin >> K;
	rep(i, 0, 50) rep(j, 0, 100001) dp[i][j] = INF;
	dp[0][0] = 0;

	// sum, mod
	priority_queue<Elem> q;
	q.push(Elem{ 0, 0, 0 });
	while (!q.empty()) {
		Elem cur = q.top(); q.pop();
		if (dp[cur.sum][cur.mod] < cur.val)continue;
		rep(x, 0, 10) {
			int nsum = cur.sum + x;
			int nmod = (cur.mod * 10 + x) % K;
			if (nsum > 45) continue;
			if (dp[nsum][nmod] > dp[cur.sum][cur.mod] + 1) {
				dp[nsum][nmod] = dp[cur.sum][cur.mod] + 1;
				q.push(Elem{ nsum, nmod, dp[nsum][nmod] });
			}
		}
	}
	rep(i, 1, 50) {
		if (dp[i][0] != INF) {
			cout << i << endl;
			return 0;
		}
	}
	return 0;
}