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>

using namespace std;

#define ll long long
constexpr ll inf = 1e9+7;

int main() {
    ll K;
    cin>>K;

    vector<pair<ll, ll>> edges(K);
    for (ll k = 0; k < K; k++) {
        edges[k].first = (k+1)%K;
        edges[k].second = (K*10)%K;
    }

    // Dijkstra
    // priority_queue<(cost, to)>
    vector<ll> costs(K, inf);
    priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> q;
    q.push(make_pair(1, 1));
    while (!q.empty()) {
        auto p = q.top();
        q.pop();
        if (costs[p.second] < p.first) continue;
        costs[p.second] = p.first;
        if (p.second == 0) {
            cout<<p.first<<endl;
            return 0;
        }
        if (costs[edges[p.second].first] > p.first + 1)
            q.push(make_pair(p.first + 1, edges[p.second].first));
        if (costs[edges[p.second].second] > p.first)
            q.push(make_pair(p.first, edges[p.second].second));
    }
}