using System; using System.Collections.Generic; using System.Linq; static class EightDigits { static void Main() { var K = int.Parse(Console.ReadLine().Trim()); var ans = Enumerate(Enumerable.Range(1, 8)) .Select(x => int.Parse(string.Join("", x))) .Where(y => y % K == 0) .Count(); Console.WriteLine(ans); } static IEnumerable Enumerate(IEnumerable items) { if (items.Count() == 1) { yield return new T[] { items.First() }; yield break; } foreach (var item in items) { var leftside = new T[] { item }; var unused = items.Except(leftside); foreach (var rightside in Enumerate(unused)) yield return leftside.Concat(rightside).ToArray(); } } }