import itertools K = int(input()) S = '12345678' ans=0 for s in itertools.permutations(S): if int(''.join(map(str, s)))%K==0: ans+=1 print(ans)