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: Wrong Answer)

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

class CompetitiveProgrammingProgram
{
    static long Gcd(long a, long b)
    {
        long[] v = new long[] { a, b };
        while (v[1] != 0) { v = new long[] { v[1], v[0] % v[1] }; }
        return v[0];
    }
    static long Lcm(long a, long b)
    {
        return a / Gcd(a, b) * b;
    }
    class Node<T>
    {
        private int m_height;
        private Node<T> m_parent;
        public T Item { get; private set; }
        public Node(T item)
        {
            m_height = 0;
            m_parent = this;
            Item = item;
        }
        public Node<T> Find()
        {
            if (m_parent == this) { return this; }

            Node<T> parent = m_parent.Find();
            m_parent = parent;
            return parent;
        }
        public static void Unite(Node<T> a, Node<T> b)
        {
            Node<T> p = a.Find();
            Node<T> q = b.Find();
            if (p.m_height < q.m_height)
            {
                p.m_parent = q;
                q.m_height = Math.Max(p.m_height + 1, q.m_height);
            }
            else
            {
                q.m_parent = p;
                p.m_height = Math.Max(q.m_height + 1, p.m_height);
            }
        }
    }
    static void Main(string[] args)
    {
        int K = int.Parse(Console.ReadLine());
        int a = 7;
        int ans = 1;
        while (a != 0)
        {
            a = a * 10 + 7;
            a %= K;
            ans++;
            if (ans > K)
            {
                ans = -1;
                break;
            }
        }
        Console.WriteLine(ans);
    }
}