Problem:
Score : 300 points

Problem Statement

Takahashi loves the number 7 and multiples of K.

Where is the first occurrence of a multiple of K in the sequence 7,77,777,\ldots? (Also see Output and Sample Input/Output below.)

If the sequence contains no multiples of K, print -1 instead.

Constraints

1 \leq K \leq 10^6

K is an integer.

Input

Input is given from Standard Input in the following format:

K

Output

Print an integer representing the position of the first occurrence of a multiple of K. (For example, if the first occurrence is the fourth element of the sequence, print 4.)

Sample Input 1

101

Sample Output 1

4

None of 7, 77, and 777 is a multiple of 101, but 7777 is.

Sample Input 2

2

Sample Output 2

-1

All elements in the sequence are odd numbers; there are no multiples of 2.

Sample Input 3

999983

Sample Output 3

999982

Solution in C# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Numerics;
using System.Text;
using static ConsoleReader;

class Program
{
    static void Main(string[] args)
    {
        var k = Read<int>();
        var mod = k % 10;
        var res = 0L;
        var count = 0;
        while (true)
        {
            var lastResDig = res % 10;
            var nextDig = 0;
            while (nextDig < 10)
            {
                if ((mod * nextDig + lastResDig) % 10 == 7) break;
                nextDig++;
            }
            if (nextDig == 10)
            {
                Console.WriteLine(-1);
                return;
            }
            count++;
            res = (res + k * nextDig) / 10;
            //Console.WriteLine($"{nextDig} : {res}");
            if (res == 0) break;
        }
        Console.WriteLine(count);
        //Console.WriteLine(res);
        //var num = new BigInteger(7);
        //var max = 1_000_000_000;
        //for (var i = 1; i < max; i++)
        //{
        //    if (num % k == 0)
        //    {
        //        Console.WriteLine(i);
        //        return;
        //    }
        //    num = num * 10 + 7;
        //}
        //Console.WriteLine(-1);

        //var num = 777777777777;
        //var div = 2;
        //while (div <= Math.Sqrt(num) + 1)
        //{
        //    if (num % div == 0)
        //    {
        //        Console.Write($"{div} ");
        //        num = num / div;
        //    }
        //    else
        //    {
        //        div++;
        //    }
        //}
        //if (num > 1) Console.Write(num);
        //for (var div = 2; div < num / 2; div++)
        //{
        //    if (num % div == 0) Console.Write($"{div} ");
        //}
    }
}

public static class IntExtentions
{
    public static void Times(this int n, Action<int> action)
    {
        for (var i = 0; i < n; i++)
        {
            action(i);
        }
    }

    public static void Times(this (int n, int m) dimensions, Action<int, int> action)
    {
        var (n, m) = dimensions;
        n.Times(i => m.Times(j => action(i, j)));
    }
}

public static class ConsoleReader
{
    public static T Read<T>()
        where T : IConvertible
    {
        var inputBuffer = new StringBuilder();
        int currChar;

        while ((currChar = Console.Read()) != -1)
        {
            if (!char.IsWhiteSpace((char)currChar))
            {
                inputBuffer.Append((char)currChar);
            }
            else if (inputBuffer.Length != 0)
            {
                break;
            }
        }

        return (T)Convert.ChangeType(inputBuffer.ToString(), typeof(T));
    }

    public static T[] ReadArray<T>(int count)
        where T : IConvertible
    {
        if (count < 0)
        {
            throw new ArgumentOutOfRangeException(nameof(count), $"'{nameof(count)}' must be >= 0");
        }

        var result = new T[count];

        for (var i = 0; i < count; i++)
        {
            result[i] = Read<T>();
        }

        return result;
    }
}