import java.io.*; import java.util.*; public class Main { static long[] values; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); long x = sc.nextLong(); int size = sc.nextInt(); values = new long[size]; for (int i = 0; i < size; i++) { values[i] = kaijo(size - i); } int[] perm = getPerm(x, size); int[] reverse = getReverse(perm); long ans = getOrder(reverse); System.out.println(ans); } static long getOrder(int[] reverse) { ArrayList list = new ArrayList<>(); for (int i = 1; i <= reverse.length; i++) { list.add(i); } long order = 0; for (int i = 0; i < reverse.length - 1; i++) { for (int j = 0; j < list.size(); j++) { if (reverse[i] == list.get(j)) { order += values[i + 1] * j; list.remove(j); break; } } } return order; } static int[] getReverse(int[] perm) { int[] reverse = new int[perm.length]; for (int i = 1; i <= perm.length; i++) { for (int j = 0; j < perm.length; j++) { if (perm[j] == i) { reverse[i - 1] = j + 1; break; } } } return reverse; } static int[] getPerm(long order, int size) { ArrayList list = new ArrayList<>(); for (int i = 1; i <= size; i++) { list.add(i); } int[] perm = new int[size]; for (int i = 0; i < size - 1; i++) { int idx; if (values[i + 1] <= order) { idx = (int)(order / values[i + 1]); order %= values[i + 1]; } else { idx = 0; } perm[i] = list.remove(idx); } perm[size - 1] = list.get(0); return perm; } static long kaijo(int x) { if (x == 0) { return 1; } else { return x * kaijo(x - 1); } } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public String next() throws Exception { if (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }