import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); long v = sc.nextLong(); int length = sc.nextInt(); int[] values = getArray(v, length); int[] nexts = new int[length]; for (int i = 0; i < length; i++) { nexts[values[i]] = i; } System.out.println(getOrder(nexts)); } static long getOrder(int[] arr) { ArrayList list = new ArrayList<>(); for (int i = 0; i < arr.length; i++) { list.add(i); } long ans = 0; for (int i = 0; i < arr.length; i++) { long tmp = proc(arr.length - i - 1); for (int j = 0; j < list.size(); j++) { if (list.get(j) == arr[i]) { list.remove(j); break; } ans += tmp; } } return ans; } static int[] getArray(long v, int length) { int[] ans = new int[length]; ArrayList list = new ArrayList<>(); for (int i = 0; i < length; i++) { list.add(i); } for (int i = 0; i < length; i++) { long tmp = proc(length - i - 1); for (int j = 0; j < list.size(); j++) { if (tmp > v) { ans[i] = list.remove(j); break; } v -= tmp; } } return ans; } static long proc(int x) { if (x == 0) { return 1; } else { return x * proc(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 double nextDouble() throws Exception { return Double.parseDouble(next()); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }