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# (Mono 4.6.2.0) (judge verdict: Runtime Error)

using System;

namespace debug
{
    class Program
    {
        static void Main(string[] args)
        {
            ProgramC a = new ProgramC();
            a.main();

        }
    }

    //ABC077
    class ProgramA
    {
        public void main()
        {
            string s1 = Console.ReadLine();
            string s2 = Console.ReadLine();

            if (s1[0] == s2[2] && s1[1] == s2[1] && s1[2] == s2[0])
                Console.WriteLine("YES");
            else
                Console.WriteLine("NO");
        }
    }

    class ProgramB
    {
        public void main()
        {
            long n = long.Parse(Console.ReadLine());
            for (long i = 1; i < 100000; i++)
            {
                if (i * i > n)
                {
                    Console.WriteLine((i - 1) * (i - 1));
                    return;
                }

            }
        }
    }

    //ABC162
    class ProgramC
    {
        public void main()
        {
            int n = int.Parse(Console.ReadLine());
            long[] a = new long[n];
            long[] b = new long[n];
            long[] c = new long[n];

            string[] s1 = Console.ReadLine().Split(' ');
            string[] s2 = Console.ReadLine().Split(' ');
            string[] s3 = Console.ReadLine().Split(' ');

            for (int i = 0; i < n; i++)
            {
                a[i] = long.Parse(s1[i]);
                b[i] = long.Parse(s2[i]);
                c[i] = long.Parse(s3[i]);
            }

            if (n == 1)
            {
                if (a[0] < b[0] && b[0] < c[0])
                    Console.WriteLine(1);
                else
                    Console.WriteLine(0);
                return;
            }

            Array.Sort(a);
            Array.Sort(b);
            Array.Sort(c);
            long ans = 0;

            for (int i = 0; i < n; i++)
            {
                long ans1 = 0;

                if (a[0] > b[i])
                    continue;


                long min = 0;
                long max = n - 1;

                if (a[n - 1] < b[i])
                {
                    ans1 = n;
                }
                else
                {
                    while (min + 1 != max)
                    {
                        long temp = (max + min) / 2;
                        if (a[temp] < b[i])
                            min = temp;
                        else
                            max = temp;

                    }

                    if (a[max] < b[i])
                        ans1 = max + 1;
                    else
                        ans1 = min + 1;
                }

                long ans2 = 0;

                if (c[n - 1] < b[i])
                    continue;

                if (c[0] > b[i])
                {
                    ans2 = n;
                }
                else
                {
                    min = 0;
                    max = n - 1;

                    while (min + 1 != max)
                    {
                        long temp = (max + min) / 2;
                        if (c[temp] <= b[i])
                            min = temp;
                        else
                            max = temp;

                    }

                    if (c[max] <= b[i])
                        ans2 = n - min;
                    else
                        ans2 = n - min - 1;
                }
                ans += ans1 * ans2;

            }

            Console.WriteLine(ans);
        }
    }



    class ProgramD
    {
        static long[] dp;
        static long[] p;
        public void main()
        {
            string[] s = Console.ReadLine().Split(' ');
            long n = long.Parse(s[0]);
            long x = long.Parse(s[1]);

            //あらかじめ層の数とパティの数は数えておく
            dp = new long[n + 1];
            p = new long[n + 1];
            dp[0] = 1;
            p[0] = 1;
            for (int i = 0; i < n; i++)
            {
                dp[i + 1] = 3 + 2 * dp[i];
                p[i + 1] = 1 + 2 * p[i];
            }

            Console.WriteLine(dfs(n, x));
        }

        /*再帰関数で把握（レベルnのバーガーの下からx番目までにあるパティの数)*/
        static long dfs(long n, long x)
        {
            if (n == 0)
            {
                if (x >= 1)
                    return 1;
                else
                    return 0;
            }
            else if (x <= 1 + dp[n - 1])
                return dfs(n - 1, x - 1);
            else
                return p[n - 1] + 1 + dfs(n - 1, x - 2 - dp[n - 1]);

        }
    }


}