from itertools import* n=int(input()) print(sum(int(''.join(p))%n==0for p in permutations('12345678')))