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)

// fn get_primes(mut n: usize) -> Vec<usize> {
//     let mut ps = vec![];
//     let mut d = 2;
//     while d * d <= n {
//         if n % d == 0 {
//             ps.push(d);
//             while n % d == 0 {
//                 n /= d;
//             }
//         }
//         d += 1;
//     }
//     if n != 1 {
//         ps.push(n);
//     }
//     ps
// }

fn solve() {
    let k: usize = read();
    if k % 2 == 0 {
        println!("{}", "-1");
    } else {
        // let prms = get_primes(k);
        let mut res = 1;
        let mut tmp = 7 % k;
        let mut s = HashSet::new();
        let mut no = false;
        while tmp != 0 {
            if s.contains(&tmp) {
                no = true;
                break;
            }
            s.insert(tmp);
            tmp *= 10;
            tmp += 7;
            tmp %= k;
            res += 1;
        }
        if no {
            println!("{}", -1);
        } else {
            println!("{}", res);
        }
    }
    // let mut tmp = 7;
    // for i in 0..20 {
    //     tmp *= 10;
    //     tmp += 7;
    //     println!("{:?}", (tmp, get_primes(tmp)));
    // }
    // println!("{:?}", get_primes(k));
}

fn main() {
    let stack_size = 104_857_600;
    let thd = std::thread::Builder::new().stack_size(stack_size);
    thd.spawn(|| solve()).unwrap().join().unwrap();
}

// =========
#[allow(unused_imports)]
use std::cmp::{max, min, Reverse};
#[allow(unused_imports)]
use std::collections::{BinaryHeap, HashMap, HashSet};
#[allow(unused_imports)]
use std::process::exit;

#[allow(dead_code)]
const MOD: usize = 1000000007;

fn read<T: std::str::FromStr>() -> T {
    use std::io::Read;
    let stdin = std::io::stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

// =========