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 5.4.1) (judge verdict: Wrong Answer)

#include <stdio.h>
#include <memory.h>
typedef long long ll;

#define mx 100001
#define MX 260

int n,dp[MX+2][mx];

int min(int a,int b){
    if(a>b) return b;
    return a;
}

int fn(int pos,int rem){
    if(rem==0 && pos){
        return 0;
    }
    if(pos==MX) return mx;
    int ans = dp[pos][rem];
    if(ans!=-1) return ans;
    ans = mx;

    for(int i = 0; i<=9; i++){
        if(pos==0 && i==0)  continue;
        ans = min(ans, i + fn(pos+1,(rem*10 + i)%n ) );
    }
    return dp[pos][rem] = ans;
}


int main(){
//read;
//IO;
scanf("%d",&n);
int p = n,q = 0;
while(p){
    q+=p%10;
    p/=10;
}
if(p==1){
    puts("1");
    return 0;
}
memset(dp,-1,sizeof dp);
int ans = fn(0,0);
printf("%d",ans);


return 0;
}