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 <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <queue>
#include <map>
#include <numeric>
#include <unordered_map>
#include <iomanip>

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define all(x) (x).begin(),(x).end()

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

typedef long long ll;

using namespace std;




int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  //cout<<setprecision(10);
  int k;
  cin>>k;
  while(k%2==0)k/=2;
  while(k%5==0)k/=5;
  int loop=0;
  int m=1;
  vector<int>edge;
  vector<int>f(k,0);
  do {
    //edge.push_back(m);
    if(f[m]==0)f[m]=1;
    else break;
    m*=10;
    m%=k;
    loop++;
  }while(m!=1);
  int l=0;
  rep(i,k)l+=f[i];
  cout<<l<<endl;
  edge.resize(l);
  l=0;
  rep(i,k){if(f[i]){edge[l]=i;l++;}}
  //cout<<loop<<endl;
  //  cout<<k<<endl;
  if(k==1){cout<<1<<endl;return 0;}
  if(loop==k-1){cout<<2<<endl;return 0;}
  vector<int>dist(k,0);

  queue<int>que;
  que.push(1);
  while(!que.empty())
  {
    int now=que.front();que.pop();
    for(int i:edge)
    {
      int tmp=(i+now);
      if(tmp>=k)tmp-=k;
      if(dist[tmp]>0)continue;
      dist[tmp]=dist[now]+1;
      if(tmp==0)
      {
        cout<<dist[tmp]+1<<endl;
        return 0;
      }
      que.push(tmp);
    }
  }



  return 0;

}