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: Accepted)

#include <bits/stdc++.h>
const int INF=1e9;
const int MOD=1e9+7;
//const int MOD=998244353;
const long long LINF=1e18;
using namespace std;
//#define int long long
//template
typedef pair<int,int> P;
//main
signed main(){
  int K;cin>>K;
  std::vector<P> e[K];
  for(int i=0;i<K;i++){
    e[i].push_back(P((i+1)%K,1));
    e[i].push_back(P(i*10%K,0));
  }
  std::vector<int> d(K,INF);
  d[1]=0;
  deque<int> que;
  que.push_front(1);
  while(que.size()){
    int p=que.front();que.pop_front();
    for(P q:e[p]){
      if(d[q.first]>d[p]+q.second){
        d[q.first]=d[p]+q.second;
        if(q.second)que.push_back(q.first);
        else que.push_front(q.first);
      }
    }
  }
  cout<<d[0]+1<<endl;
}