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 <bits/stdc++.h>
# include <ext/pb_ds/assoc_container.hpp>
# include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long       ll;

# define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 
#define all(x)            (x).begin(),(x).end()
#define Sort(x)            sort(all(x))
#define A               first
#define B               second
#define sep             ' '
#define fast_io         ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define set_random     mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count())

const int MAX1 = 1e5+10,
         Md = 1e9+7,
		 Base = 257;

ll q, n, x, y, ans, s1, s0;

ll Solve(ll a){
	ll jav = 0;
	while (a)	jav += a%10, a /= 10;
	return jav;
}

int main(){
    fast_io;
    cin >> n;
    ans = 1e18;
    for (int i = 1; i <= 18; ++i){
    	ll adad = pow(10, i);
    	ll avval = (adad-1)/n*n+n;
    	for (int j = -1e5; j < 1e6; ++j){
    		if (avval+n*j > 0)	ans = min(ans, Solve(avval+n*j));
		}
	}
	cout << ans << endl;
    return 0;
}
/*               now or never                 */