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 Java (OpenJDK 11.0.6) (judge verdict: Accepted)

import java.io.*;
import java.util.*;
import java.math.*;
import static java.lang.Math.*;

public class Main {

    private static final FastScanner fs = new FastScanner();

    public static void main(String[] args) {
        // int t = fs.nextInt();
        int t = 1;
        for (int i = 1; i <= t; i++) {
            // System.out.print("Case #" + i + ": ");
            solve();
        }
    }

    private static void solve() {
        int k = fs.nextInt();
        int x = 7 % k;
        for (int i = 0; i <= 3 * k; i++) {
            if (x == 0) {
                System.out.println(i + 1);
                return;
            }
            x = (x * 10 + 7) % k;
        }
        System.out.println("-1");
    }

    private static class FastScanner {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer("");

        public String next() {
            while (!st.hasMoreTokens()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                } 
            }
            return st.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public int[] intArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++) a[i] = nextInt();
            return a;
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public long[] longArray(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++) a[i] = nextLong();
            return a;
        }
    }

    private static class Pair<T> {
        public T f, s;
        
        public Pair(T f, T s) {
            this.f = f;
            this.s = s;
        }

        @Override
        public int hashCode() {
            int hash = 5;
            hash = 67 * hash + Objects.hashCode(this.f);
            hash = 67 * hash + Objects.hashCode(this.s);
            return hash;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Pair<?> other = (Pair<?>) obj;
            if (!Objects.equals(this.f, other.f)) {
                return false;
            }
            return Objects.equals(this.s, other.s);
        } 
    }
    
    private static class CPair<T extends Comparable<T>> implements Comparable<CPair> {
        public T f, s;
        
        public CPair(T f, T s) {
            this.f = f;
            this.s = s;
        }
        
        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final CPair<?> other = (CPair<?>) obj;
            if (!Objects.equals(this.f, other.f)) {
                return false;
            }
            return Objects.equals(this.s, other.s);
        } 
       
        @Override
        public int compareTo(CPair t) {
            CPair<T> other = (CPair<T>) t;
            if (f.compareTo(other.f) < 0) return -1;
            if (f.compareTo(other.f) > 0) return 1;
            return s.compareTo(other.s);
        }
        
    }

}