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>
using namespace std;
#define ll long long
#define il inline
#define outy printf("Yes\n")
#define outn printf("No\n")
#define outf printf("First\n")
#define outs printf("Second\n")
#define FOR(a,b,c) for(int a=b;a<=c;a++)
#define FORD(a,b,c) for(int a=b;a>=c;a--)
#define FORL(a,b) for(int a=head[b];a;a=nxt[a])
#define in(a)  a=read()
#define in2(a,b)  a=read(),b=read()
#define in3(a,b,c)  a=read(),b=read(),c=read()
#define in4(a,b,c,d)  a=read(),b=read(),c=read(),d=read()
#define inl(a)  a=readl()
#define inl2(a,b)  a=readl(),b=readl()
#define inl3(a,b,c)  a=readl(),b=readl(),c=readl()
#define inl4(a,b,c,d)  a=readl(),b=readl(),c=readl(),d=readl()
#define out(a) printf("%d\n",a)
#define out2(a,b) printf("%d %d\n",a,b)
#define out3(a,b,c) printf("%d %d %d\n",a,b,c)
#define out4(a,b,c,d) printf("%d %d %d %d\n",a,b,c,d)
#define outl(a) printf("%lld\n",a)
#define outl2(a,b) printf("%lld %lld\n",a,b)
#define outl3(a,b,c) printf("%lld %lld %lld\n",a,b,c)
#define outl4(a,b,c,d) printf("%lld %lld %lld %lld\n",a,b,c,d)
#define clr(x,y) memset(x,y,sizeof(x))
inline int read(){
    char c;int s=0,t=1;
    while(!isdigit(c=getchar()))if(c=='-')t=-1;
    do{s=s*10+c-'0';}while(isdigit(c=getchar()));
    return s*t;
}
inline ll readl(){
    char c;ll s=0;int t=1;
    while(!isdigit(c=getchar()))if(c=='-')t=-1;
    do{s=s*10+c-'0';}while(isdigit(c=getchar()));
    return s*t;
}
/*------------------------------------------------------------*/
#define mod 1000000007
#define maxn 100010
/*------------------------------------------------------------*/
#define gg i1+i2+i3+i4+i5+i6+i7+i8+i9
int main(){
	int n;
	in(n);
	int k=maxn;
	FOR(i1,0,9){
		ll c1=i1;
		int a=i1;
		if(i1>=k)break;
		FOR(i2,0,9){
			ll c2=c1*10+i2;
			a+=i2;
			if(i1+i2>=k)break;
			FOR(i3,0,9){
				ll c3=c2*10+i3;
				a+=i3;
				if(i1+i2+i3>=k)break;
				FOR(i4,0,9){
					ll c4=c3*10+i4;
					a+=i4;
					if(i1+i2+i3+i4>=k)break;
					FOR(i5,0,9){
						ll c5=c4*10+i5;
						a+=i5;
						if(i1+i2+i3+i4+i5>=k)break;
						FOR(i6,0,9){
							ll c6=c5*10+i6;
							a+=i6;
							if(i1+i2+i3+i4+i5+i6>=k)break;
							FOR(i7,0,9){
								ll c7=c6*10+i7;
								a+=i7;
								if(i1+i2+i3+i4+i5+i6+i7>=k)break;
								FOR(i8,0,9){
									ll c8=c7*10+i8; 
									a+=i8;
									if(i1+i2+i3+i4+i5+i6+i7+i8>=k)break;
									FOR(i9,0,9){
										ll c9=c8*10+i9;
										a+=i9;
										if(gg>=k)break;
										if(c9!=0&&c9%(ll)n==0&&gg<k)k=gg;
										a-=i9;
									}
									a-=i8;
								}
								a-=i7;
							}
							a-=i6;
						}
						a-=i5;
					}
					a-=i4;
				}
				a-=i3;
			}
			a-=i2;
		}
	}
	out(k);
}