from itertools import permutations K = int(input()) ans = 0 for i in permutations("12345678"): ans += int("".join(i)) % K == 0 print(ans)