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 Rust (1.42.0) (judge verdict: Accepted)


use std::io::{self, BufRead, BufReader};

fn main() -> io::Result<()>{
    let mut stdin = BufReader::new(io::stdin());
    let mut buf = String::new();

    stdin.read_line(&mut buf)?;

    let k = buf.split_whitespace().map(|token| token.parse::<u64>().unwrap()).collect::<Vec::<_>>()[0];
    let mut i = f64::log10((k as f64) * 7.0/9.0 + 1.0) as u32 + 1;
    #[allow(non_snake_case)]
    let mut A = (7 * (10_u64.pow(i) - 1) / 9) % k;
    let mut tmp = 10_u64.pow(i) % k;

    while i <= 9000000{
	if A == 0{
	    println!("{}",i);
	    return Ok(())
	}
	A += ((tmp * 7) % k);
	A %= k;
	tmp *= 10;
	tmp %= k;
	i += 1;
    }
    println!("-1");
    Ok(())
}


fn pow_mod(n : u64, e : u32, m : u64) -> u64{
    let mut e = e;
    #[allow(non_snake_case)]
    let mut A = n % m;
    let mut res = 1;
    while e > 0{
	if e & 1 == 1{
	    res = (res * (A % m)) % m;
	}
	A = (A * (A % m)) % m;
	e >>= 1;
    }
    res
}