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 <cstdio>
#include <map>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <set>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;


#define pii pair<int,int>

const int MX = 100500;
const int SZ = 2400;
int z[SZ];

int sp[100500];
int dst[100500];

int solve(int k, int max_cnt){
    z[0] = 1;
    for(int i = 1; i < max_cnt; i++){
        z[i] = z[i - 1] * 10;
        z[i] %= k;
    }
    for(int i = 0; i < max_cnt; i++){
        sp[i] = z[i];
        dst[ z[i] ] = 1;
    }
    int x = 0;
    int y = max_cnt;
    while(x < y){
        int state = sp[x++];
        int d = dst[state];
        for(int i = 0; i < max_cnt; i++){
            int n_state = state + z[i];
            if(n_state >= k) n_state -= k;
            if(dst[n_state] == 0){
                dst[n_state] = d + 1;
                sp[y++] = n_state;
            }
        }
    }
    return dst[0];
}


void brute(){
    int z = 0;
    for(int k = 1; k <= 100000; k++){
        int t = solve(k, 200);
        int t2 = solve(k, 2000);
        if(t != t2) {
            cout << "Shit " << k << ' ' <<  t << ' ' << t2 << endl;
        }else
            cerr << k << " OK\r";
    }
    cout << z << endl;
}


int main(int argc, char** argv)
{
    //freopen("/Users/ozzy/Documents/in.txt", "r", stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(0);
//    solve(79992);
    //brute();

    int k = 99999;
    cin >> k;

    cout << solve(k, 1600) << endl;
    return 0;
}

// 1 4 9 6 5 6