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)

//This is getting accepted!
// I HATE BUG
// God Of The Bugs
// 12/11/2016
#include<bits/stdc++.h>
 
using namespace std;
 
#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORd(i, a, b) for (int i = (a) - 1; i >= (b); i--)
#define FORall(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define FI first
#define SE second
#define pb push_back
#define mp make_pair
#define ll long long
#define sz(a) ((int)(a).size())
#define __builtin_popcount __builtin_popcounll
#define ld long double
 
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef pair<int, pii> ppi;
 
const double PI = acos(0) * 2;
const double EPS = 1e-8;
const ll MOD = 1e9 + 7;
const int MAXN = 1e5 + 5;
const int oo = 1e9;
const double foo = 1e30;
 
template<class T> int getbit(T s, int i) { return (s >> i) & 1; }
template<class T> T onbit(T s, int i) { return s | (T(1) << i); }
template<class T> T offbit(T s, int i) { return s & (~(T(1) << i)); }
template<class T> int cntbit(T s) { return __builtin_popcounll(s);}
template<class T> T sqr(T x) { return x * x; }
 
inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}
inline void submod(int& a, int val, int p = MOD) {if ((a = (a - val)) < 0) a += p;}
inline int mult(int a, int b, int p = MOD) {return (ll) a * b % p;}
 
int K, dp[2][MAXN];
 
ll cal(ll x) {
    ll res = 0;
    while (x) {
        res += x % 10;
        x /= 10;
    }
    return res;
}
 
int main() {
//#ifndef ONLINE_JUDGE
//    freopen("inp.txt", "r", stdin);
////    freopen("out.txt", "w", stdout);
//#endif
 
    cin >> K;
    
    FOR(i, 0, 2) {
        FOR(j, 0, K) {
            dp[i][j] = oo;
        }
    }
    
    int lv = 0;
    
    FOR(i, 1, 10) {
        dp[0][i % K] = min(dp[0][i % K], i);
    }
        
    int cur = 1;
    int ans = oo;
    FOR(i, 1, 1700) {
        cur = cur * 10 % K;
        FOR(j, 0, min(K, 1700 * 9 + 1)) dp[lv ^ 1][j] = oo;
        FOR(j, 0, min(K, 1700 * 9 + 1)) {
            FOR(t, 0, 10) {
                int rem = cur * t % K;
//                if (i == 1 && (j + rem) % K == 92) cout << j << endl;
                dp[lv ^ 1][(j + rem) % K] = min(dp[lv ^ 1][(j + rem) % K], dp[lv][j] + t);
            }
        }
        FOR(t, 1, 10) {
            int rem = cur * t % K;
            dp[lv ^ 1][rem] = min(dp[lv ^ 1][rem], t);
        }
        ans = min(ans, dp[lv ^ 1][0]);
        lv ^= 1;
    }
//    cout << dp[0][2] << endl;
    cout << ans;
 
    return 0;
}