import java.io.*; import java.util.*; import java.util.stream.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); long n = sc.nextLong(); int s = sc.nextInt(); int[] base = new int[s]; getArray(0, base, new boolean[s], n); int[] reverse = new int[s]; getReverse(base, reverse); System.out.println(getIdx(0, reverse, new boolean[s])); } static void getArray(int idx, int[] arr, boolean[] used, long value) { long f = fact(arr.length - idx - 1); for (int i = 0; i < used.length; i++) { if (used[i]) { continue; } if (value >= f) { value -= f; } else { used[i] = true; arr[idx] = i; getArray(idx + 1, arr, used, value); } } } static void getReverse(int[] base, int[] reverse) { for (int i = 0; i < base.length; i++) { reverse[base[i]] = i; } } static long getIdx(int idx, int[] arr, boolean[] used) { long f = fact(arr.length - idx - 1); long ans = 0; for (int i = 0; i < arr.length; i++) { if (used[i]) { continue; } if (i == arr[idx]) { used[i] = true; ans += getIdx(idx + 1, arr, used); break; } else { ans += f; } } return ans; } static long fact(int x) { long ans = 1; for (int i = 2; i <= x; i++) { ans *= i; } return ans; } } 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(); } }