using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static long[] NList => ReadLine().Split().Select(long.Parse).ToArray(); public static void Main() { Solve(); } static void Solve() { counts[1] = 1; for (var i = 2; i < counts.Length; ++i) counts[i] = counts[i - 1] * i; var c = NList; WriteLine(Reverse(c[0], (int)c[1])); } static long[] counts = new long[21]; static long Reverse(long n, int s) { var list = new List(); for (var i = 0; i < s; ++i) list.Add(i); var perm = GetPerm(n, list); var rev = new int[s].ToList(); for (var i = 0; i < s; ++i) rev[perm[i]] = i; return GetIndex(rev); } static List GetPerm(long n, List ints) { if (ints.Count == 0) return ints; for (var i = 0; i < ints.Count; ++i) { if (n - i * counts[ints.Count - 1] < counts[ints.Count - 1]) { var first = ints[i]; ints.RemoveAt(i); var ans = new List{ first }; ans.AddRange(GetPerm(n - i * counts[ints.Count], ints)); return ans; } } return ints; } static long GetIndex(List perm) { if (perm.Count == 0) return 0; var idx = 0; for (var i = 1; i < perm.Count; ++i) if (perm[0] > perm[i]) ++idx; return idx * counts[perm.Count - 1] + GetIndex(perm.Skip(1).ToList()); } }