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)

#[allow(unused_imports)]
use proconio::{input, marker::*};

#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};

#[allow(unused_imports)]
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        #[cfg(debug_assertions)]
        writeln!(&mut std::io::stderr(), concat!("[DEBUG] ", $(stringify!($a), "={:?} "),*), $($a),*).unwrap();
    }
}
use self::modint::ModInt;

mod modint {
    use std;
    #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
    pub struct ModInt {
        pub value: usize,
        module: usize,
    }
    impl ModInt {
        pub fn new(value: usize, module: usize) -> Self {
            ModInt {
                value: value % module,
                module: module,
            }
        }
    }
    impl std::ops::Add for ModInt {
        type Output = Self;
        fn add(self, other: Self) -> Self {
            let mut value = self.value + other.value;
            if value > self.module {
                value -= self.module;
            }
            ModInt::new(value, self.module)
        }
    }
    impl std::ops::Add<usize> for ModInt {
        type Output = Self;
        fn add(self, other: usize) -> Self {
            let mut value = self.value + other;
            if value > self.module {
                value -= self.module;
            }
            ModInt::new(value, self.module)
        }
    }
    impl std::ops::Sub for ModInt {
        type Output = Self;
        fn sub(self, other: Self) -> Self {
            let mut value = self.value;
            if value < other.value {
                value += self.module;
            }
            value -= other.value;
            ModInt::new(value, self.module)
        }
    }
    impl std::ops::Sub<usize> for ModInt {
        type Output = Self;
        fn sub(self, other: usize) -> Self {
            let mut value = self.value;
            if value < other {
                value += self.module;
            }
            value -= other;
            ModInt::new(value, self.module)
        }
    }
    impl std::ops::Mul for ModInt {
        type Output = Self;
        fn mul(self, other: Self) -> Self {
            let value = self.value * other.value % self.module;
            ModInt::new(value, self.module)
        }
    }
    impl std::ops::Mul<usize> for ModInt {
        type Output = Self;
        fn mul(self, other: usize) -> Self {
            let value = self.value * other % self.module;
            ModInt::new(value, self.module)
        }
    }
    impl std::ops::Div for ModInt {
        type Output = Self;
        fn div(self, other: Self) -> Self {
            let mut exp = self.module - 2;
            let mut value = self.value;
            let mut ovalue = other.value;
            while exp > 0 {
                if exp % 2 == 1 {
                    value = value * ovalue % self.module;
                }
                ovalue = ovalue * ovalue % self.module;
                exp /= 2;
            }
            ModInt::new(value, self.module)
        }
    }
    impl std::ops::Div<usize> for ModInt {
        type Output = Self;
        fn div(self, other: usize) -> Self {
            let mut exp = self.module - 2;
            let mut value = self.value;
            let mut ovalue = other;
            while exp > 0 {
                if exp % 2 == 1 {
                    value = value * ovalue % self.module;
                }
                ovalue = ovalue * ovalue % self.module;
                exp /= 2;
            }
            ModInt::new(value, self.module)
        }
    }
    impl std::ops::AddAssign for ModInt {
        fn add_assign(&mut self, other: Self) {
            *self = *self + other;
        }
    }
    impl std::ops::AddAssign<usize> for ModInt {
        fn add_assign(&mut self, other: usize) {
            *self = *self + other;
        }
    }
    impl std::ops::SubAssign for ModInt {
        fn sub_assign(&mut self, other: Self) {
            *self = *self - other;
        }
    }
    impl std::ops::SubAssign<usize> for ModInt {
        fn sub_assign(&mut self, other: usize) {
            *self = *self - other;
        }
    }
    impl std::ops::MulAssign for ModInt {
        fn mul_assign(&mut self, other: Self) {
            *self = *self * other;
        }
    }
    impl std::ops::MulAssign<usize> for ModInt {
        fn mul_assign(&mut self, other: usize) {
            *self = *self * other;
        }
    }
    impl std::ops::DivAssign for ModInt {
        fn div_assign(&mut self, other: Self) {
            *self = *self / other;
        }
    }
    impl std::ops::DivAssign<usize> for ModInt {
        fn div_assign(&mut self, other: usize) {
            *self = *self / other;
        }
    }
    impl std::fmt::Debug for ModInt {
        fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
            write!(f, "{}", self.value)
        }
    }
    impl std::fmt::Display for ModInt {
        fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
            write!(f, "{}", self.value)
        }
    }
}

fn main() {
    input! {
        k: usize,
    }

    if k % 2 == 0 || k % 5 == 0 {
        println!("-1");
        return;
    }
    let mut val = ModInt::new(7, k);
    for i in 0.. {
        if val.value == 0 {
            println!("{}", i + 1);
            return;
        }
        val = val * 10 + 7;
    }
}