using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray(); public static void Main() { Solve(); } static void Solve() { var k = NN; var ans = 0; Permutate(8, list => { var n = 0; for (var i = 0; i < 8; ++i) n = n * 10 + list[i] + 1; if (n % k == 0) ++ans; }); WriteLine(ans); } static void Permutate(int n, Action action) { var seed = new int[n]; for (var i = 0; i < n; ++i) seed[i] = i; var used = new bool[n]; var perm = new int[n]; Permutate(0, seed, perm, used, action); } static void Permutate(int pos, int[] seed, int[] perm, bool[] used, Action action) { if (pos == used.Length) action(perm); for (var i = 0; i < used.Length; ++i) { if (used[i]) continue; perm[pos] = seed[i]; used[i] = true; Permutate(pos + 1, seed, perm, used, action); used[i] = false; } } }