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++ (GCC 9.2.1) (judge verdict: Accepted)

#include<iostream>
#include<vector>
#include<deque>
using namespace std;

int K;
vector<pair<int,int> > G[100000];
int d[100000];

void bfs(int v){
    fill(d,d+K,-1);
    deque<pair<int,int> > deq;
    deq.push_back(make_pair(v,0));
    pair<int,int> pa;
    int from,to,cost,zeroone;
    int i;
    while(!deq.empty()){
        pa= deq.front();
        deq.pop_front();
        from=pa.first;
        cost=pa.second;
        if(d[from]>=0) continue;
        d[from]=cost;
        for(i=0; i<G[from].size(); i++){
            pa=G[from][i];
            to=pa.first;
            zeroone=pa.second;
            if(d[to]<0){
                if(zeroone==0){
                    deq.push_front(make_pair(to,cost));
                }else{
                    deq.push_back(make_pair(to,cost+1));
                }
            }
        }
    }

}

int main(){
    cin >> K;
    int i;
    for(i=0; i<K-1; i++){
        G[i].push_back(make_pair((10*i)%K,0));
        G[i].push_back(make_pair(i+1,1));
    }
    G[K-1].push_back(make_pair((10*(K-1))%K,0));
    G[K-1].push_back(make_pair(0,1));
    bfs(1);
    cout << d[0]+1 << endl;
    system("pause");
    return 0;
}