K = gets.to_i puts [* ?1 .. ?8].permutation(8).count { |digits| digits.join.to_i % K == 0 }