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 <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

#define FOR(i, j, k) for (int i = j; i < k; i++)
#define REP(i, j) FOR(i, 0, j)
typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;

struct edge {
  int from, to, cost;
  edge(int f, int t, int c) : from(f), to(t), cost(c) {}
};

typedef pair<long long, int> P; // firstは最短距離、secondは頂点の番号
vector<edge> es[100001];

int main() {

  ll K;
  cin >> K;

  REP(i, K) {
    int to = (i + 1) % K;
    es[i].emplace_back(edge(i, to, 1));
  }

  REP(i, K) {
    int to = (i * 10) % K;
    es[i].emplace_back(edge(i, to, 0));
  }

  vi d(K, (1 << 31));
  d[1] = 0;
  priority_queue<P, vector<P>, greater<P>> pq;
  pq.push(P(0, 1));

  while (!pq.empty()) {
    P p = pq.top();
    pq.pop();

    for (auto e : es[p.second]) {
      if (d[e.to] == (1 << 31)) {
        d[e.to] = d[e.from] + e.cost;
        pq.push(P(e.cost, e.to));
      }
    }
  }

  cout << d[0] + 1 << endl;

  return 0;
}