import java.io.*; import java.util.*; import java.util.function.*; import java.util.stream.*; public class Main { static long[] facts; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); long x = sc.nextLong(); int n = sc.nextInt(); facts = new long[n]; facts[0] = 1; for (int i = 1; i < n; i++) { facts[i] = facts[i - 1] * i; } System.out.println(getCount(getReverse(getArray(x, n)))); } static int[] getArray(long value, int size) { int[] ans = new int[size]; boolean[] used = new boolean[size]; for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (used[j]) { continue; } if (value < facts[size - i - 1]) { used[j] = true; ans[i] = j; break; } value -= facts[size - i - 1]; } } return ans; } static int[] getReverse(int[] arr) { int[] ans = new int[arr.length]; for (int i = 0; i < arr.length; i++) { ans[arr[i]] = i; } return ans; } static long getCount(int[] arr) { long ans = 0; boolean[] used = new boolean[arr.length]; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr.length; j++) { if (used[j]) { continue; } if (arr[i] == j) { used[j] = true; break; } ans += facts[arr.length - i - 1]; } } return ans; } } class Scanner { BufferedReader br; StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); } catch (Exception e) { } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { try { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } } catch (Exception e) { e.printStackTrace(); } finally { return st.nextToken(); } } }