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.util.Scanner;

public class Main {
	public static void main(String[] args) {

		int sum = 0;
		int count = 0;
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		for(long i = 0 ; i < n ; i++) {
			count++;
			sum += pow(10 , i , n) * 7;
			sum += n;
            sum %= n;
			if(sum == 0) {
				System.out.println(count);
				return;
			}

		}
		System.out.println(-1);

	}
	public static long pow(long a,long b,int M) {
        long ret = 1;
        long tmp = a;
        while(b>0) {
            if((b&1)==1) ret = (ret * tmp) % M;  //2進数によるべき乗の高速計算
            tmp = (tmp * tmp) % M;
            b = b>>1;
        }
        return ret;
    }
}