K = gets.to_i ans = 0 [1, 2, 3, 4, 5, 6, 7, 8].permutation(8) do |nums| x = 0 base = 1 nums.each do |n| x += n * base x %= K base *= 10 end ans += 1 if x == 0 end puts ans