let nextPermutation (xs: char array) = if xs.Length < 2 then false else let high = xs.Length - 1 let mutable i = high while i > 0 && xs.[i-1] >= xs.[i] do i <- i - 1 if i = 0 then false else let mutable j = high while j >= i && xs.[j] <= xs.[i-1] do j <- j - 1 let mutable tmp = xs.[j] xs.[j] <- xs.[i-1] xs.[i-1] <- tmp let mutable d = 0 while i + d < high - d do tmp <- xs.[i+d] xs.[i+d] <- xs.[high-d] xs.[high-d] <- tmp d <- d + 1 true let k = stdin.ReadLine() |> int let mutable cnt = if 12345678 % k = 0 then 1 else 0 let arr = "12345678".ToCharArray() while nextPermutation arr do let n = arr |> System.String.Concat |> int if n % k = 0 then cnt <- cnt + 1 cnt |> printfn "%d"