-- https://github.com/minoki/my-atcoder-solutions
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE NoStarIsType #-}
{-# LANGUAGE ScopedTypeVariables #-}
import Data.Char (isSpace)
import Data.Int (Int64)
import Data.List (unfoldr)
import Control.Monad
import qualified Data.Vector.Unboxed as U
import qualified Data.Vector.Unboxed.Mutable as UM
import qualified Data.ByteString.Char8 as BS
import qualified Test.QuickCheck as QC
import Data.Proxy
import GHC.TypeNats (Nat, KnownNat, SomeNat(..), someNatVal, natVal, type (^), type (+))
import Control.Exception (assert)

naive :: Int -> Int
naive k | k `rem` 2 == 0 || k `rem` 5 == 0 = -1
        | otherwise = 1 + length (takeWhile (\x -> x `rem` toInteger k /= 0) $ iterate (\x -> x * 10 + 7) 7)

naive2 :: Int -> Int
naive2 k | k `rem` 2 == 0 || k `rem` 5 == 0 = -1
         | otherwise = 1 + length (takeWhile (\x -> x /= 0) $ iterate (\x -> (x * 10 + 7) `rem` k) (7 `rem` k))

prop_n :: QC.Positive Int -> Bool
prop_n (QC.Positive k) = naive2 k == naive k

factorOut3 :: Int -> (Int, Int)
factorOut3 = go 0
  where
    go !acc !n = case n `quotRem` 3 of
                   (q,0) -> go (acc + 1) q
                   _ -> (n, acc)

prop_factorOut3 :: Int -> Int -> Bool
prop_factorOut3 x y = factorOut3 (3^x * (3 * y + 1)) == (3 * y + 1, x)

-- a_0 = 0
-- a_{n+1} = 10 * a_n + 7  mod 9
--         = a_n + 7  mod 9
-- a_n = 7 * n  mod 9
solve3 :: Int -> Int
solve3 threes = 3 ^ threes

solveNot3 :: Int -> Int
solveNot3 1 = 1
solveNot3 m = assert (gcd m 3 == 1) $
              case someNatVal (fromIntegral m) of
                SomeNat (_ :: Proxy m) -> 1 + discreteLogarithm (10 :: IntMod m) (1/10)

-- a_0 = 0
-- a_{n+1} = 10 * a_n + 7
-- a_{n+1} - t = 10 * (a_n + (7 - t) / 10)
-- - t = (7 - t) / 10
-- 0 = (7 + 9 * t) / 10
-- t = -7/9
-- a_{n+1} + 7/9 = 10 * (a_n + (63 + 7) / 90)
-- a_{n+1} + 7/9 = 10 * (a_n + 7/9)
-- (1/10)^{n+1} * (a_{n+1} + 7/9) = (1/10)^n * (a_n + 7/9)
-- (1/10)^n * (a_n + 7/9) = 0 + 7/9 = 7/9
-- a_n + 7/9 = 7/9 * 10^n
-- a_n = 7/9 * (10^n - 1)
-- a_n = 0 <=> 10^n = 1 mod m
solve :: Int -> Int
solve k | k `rem` 2 == 0 || k `rem` 5 == 0 = -1
        | (k', threes) <- factorOut3 k = case k' `quotRem` 7 of
                                           (k'',0) -> lcm (solve3 threes) (solveNot3 k'')
                                           (_,_) -> lcm (solve3 threes) (solveNot3 k')

prop :: QC.Positive Int -> Bool
prop (QC.Positive k) = solve k == naive k

main = do
  k <- readLn @Int
  print $ naive2 k

--
-- Modular Arithmetic
--

newtype IntMod (m :: Nat) = IntMod { unwrapN :: Int64 } deriving (Eq)

instance Show (IntMod m) where
  show (IntMod x) = show x

instance KnownNat m => Num (IntMod m) where
  t@(IntMod x) + IntMod y
    | x + y >= modulus = IntMod (x + y - modulus)
    | otherwise = IntMod (x + y)
    where modulus = fromIntegral (natVal t)
  t@(IntMod x) - IntMod y
    | x >= y = IntMod (x - y)
    | otherwise = IntMod (x - y + modulus)
    where modulus = fromIntegral (natVal t)
  t@(IntMod x) * IntMod y = IntMod ((x * y) `rem` modulus)
    where modulus = fromIntegral (natVal t)
  fromInteger n = let result = IntMod (fromInteger (n `mod` fromIntegral modulus))
                      modulus = natVal result
                  in result
  abs = undefined; signum = undefined

{-# RULES
"^9/Int" forall x. x ^ (9 :: Int) = let u = x; v = u * u * u in v * v * v
"^9/Integer" forall x. x ^ (9 :: Integer) = let u = x; v = u * u * u in v * v * v
 #-}

fromIntegral_Int64_IntMod :: KnownNat m => Int64 -> IntMod m
fromIntegral_Int64_IntMod n = result
  where
    result | 0 <= n && n < modulus = IntMod n
           | otherwise = IntMod (n `mod` modulus)
    modulus = fromIntegral (natVal result)

{-# RULES
"fromIntegral/Int->IntMod" fromIntegral = fromIntegral_Int64_IntMod . (fromIntegral :: Int -> Int64) :: Int -> IntMod (10^9 + 7)
"fromIntegral/Int64->IntMod" fromIntegral = fromIntegral_Int64_IntMod :: Int64 -> IntMod (10^9 + 7)
 #-}

---

exEuclid :: (Eq a, Integral a) => a -> a -> (a, a, a)
exEuclid !f !g = loop 1 0 0 1 f g
  where loop !u0 !u1 !v0 !v1 !f 0 = (f, u0, v0)
        loop !u0 !u1 !v0 !v1 !f g =
          case divMod f g of
            (q,r) -> loop u1 (u0 - q * u1) v1 (v0 - q * v1) g r

instance KnownNat m => Fractional (IntMod m) where
  recip t@(IntMod x) = IntMod $ case exEuclid x modulus of
    (1,a,_) -> a `mod` modulus
    (-1,a,_) -> (-a) `mod` modulus
    _ -> error "not invertible"
    where modulus = fromIntegral (natVal t)
  fromRational = undefined

-- Properties:
-- * base^(discreteLogarithm base x) == x
-- * let l = discreteLogarithm base x in filter (base^t == x) [0 .. l]) == l
discreteLogarithm :: KnownNat m => IntMod m -> IntMod m -> Int
discreteLogarithm base x = go 0 1
  where
    -- Naive algorithm
    go n y | y == x = n
           | otherwise = go (n + 1) (y * base)
