from itertools import permutations K = int(input()) ans = 0 for p in permutations('12345678'): if int(''.join(p)) % K == 0: ans += 1 print(ans)