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