import java.io.*; import java.util.*; import java.util.stream.*; public class Main { static long[] fact; public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); long n = sc.nextLong(); int length = sc.nextInt(); fact = new long[length + 1]; fact[0] = 1; for (int i = 1; i <= length; i++) { fact[i] = fact[i - 1] * i; } int[] order = new int[length]; getOrder(length - 1, n, order, new boolean[length]); int[] reverse = new int[length]; for (int i = length - 1; i >= 0; i--) { reverse[length - order[i] - 1] = length - i - 1; } System.out.println(getNumber(length - 1, reverse, new boolean[length])); } static void getOrder(int idx, long value, int[] order, boolean[] used) { if (idx < 0) { return; } long count = value / fact[idx]; for (int i = 0; i < used.length; i++) { if (used[i]) { continue; } if (count == 0) { used[i] = true; order[idx] = i; getOrder(idx - 1, value % fact[idx], order, used); return; } count--; } } static long getNumber(int idx, int[] order, boolean[] used) { if (idx < 0) { return 0; } int count = 0; for (int i = 0; i < used.length; i++) { if (used[i]) { continue; } if (i == order[idx]) { used[i] = true; return fact[idx] * count + getNumber(idx - 1, order, used); } count++; } return 0; } } class Utilities { static String arrayToLineString(Object[] arr) { return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n")); } static String arrayToLineString(int[] arr) { return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new)); } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public int[] nextIntArray() throws Exception { return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }