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# (.NET Core 3.1.201) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AtCoder.Abc.D
{
    public class Question
    {
        public static void Main(string[] args)
        {
            new Question().Solve(new ConsoleInput(Console.In, ' '));
        }
        public long DigitSum(long n)
        {
            var res = 0L;
            while(n != 0)
            {
                res += n % 10;
                n /= 10;
            }
            return res;
        }
        long[] memo;
        public long rec(long n)
        {
            if (memo[n] != 0) return memo[n];
            if (n % 5 == 0) return memo[n] = rec(n / 5);
            else if (n % 2 == 0) return memo[n] = rec(n / 2);
            if(n <= 1000)
            {
                long res = n;
                for (int i = 1; i <= 1000; i++)
                {
                    res = Math.Min(res, DigitSum(i * n));
                }
                return memo[n] = res;
            }
            return memo[n] = DigitSum(n);
        }

        public void Solve(ConsoleInput cin)
        {
            long k = cin.ReadLong;
            memo = new long[k + 1];
            Console.WriteLine(rec(k));
        }

        public class ConsoleInput
        {
            private readonly System.IO.TextReader _stream;
            private char _separator = ' ';
            private Queue<string> inputStream;
            public ConsoleInput(System.IO.TextReader stream, char separator = ' ')
            {
                this._separator = separator;
                this._stream = stream;
                inputStream = new Queue<string>();
            }
            public string Read
            {
                get
                {
                    if (inputStream.Count != 0) return inputStream.Dequeue();
                    string[] tmp = _stream.ReadLine().Split(_separator);
                    for (int i = 0; i < tmp.Length; ++i)
                        inputStream.Enqueue(tmp[i]);
                    return inputStream.Dequeue();
                }
            }
            public string ReadLine { get { return _stream.ReadLine(); } }
            public int ReadInt { get { return int.Parse(Read); } }
            public long ReadLong { get { return long.Parse(Read); } }
            public double ReadDouble { get { return double.Parse(Read); } }
            public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret; }
            public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; }
            public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; }
        }
    }
}