#include int next_permutation(int N, int p[]) { static int i, j, q[10] = {}; for (i = N; i > 1; i--) { q[p[i]] = 1; if (p[i] > p[i-1]) break; } if (i == 1) return 0; q[p[i-1]] = 1; for (j = p[i-1] + 1; q[j] == 0; j++); p[i-1] = j; q[j] = 0; for (j = 1; i <= N; i++) { for (; q[j] == 0; j++); p[i] = j; q[j++] = 0; } return 1; } int main() { int i, k, K, p[10], ans = 0; scanf("%d", &K); for (i = 1; i <= 8; i++) p[i] = i; do { for (i = 1, k = 0; i <= 8; i++) k = k * 10 + p[i]; if (k % K == 0) ans++; } while (next_permutation(8, p)); printf("%d\n", ans); fflush(stdout); return 0; }