#include using namespace std; using ll = long long; int main(){ ll n, s; cin >> n >> s; vector fact(s + 1); fact[0] = 1; for(int i = 1; i <= s; i++)fact[i] = i * fact[i - 1]; vector p(s), p2(s); function dfs = [&](int i, int S, ll rem){ if(i == s)return; ll v = 0; int cand = -1; for(int j = 0; j < s; j++){ if(S >> j & 1)continue; if(rem >= fact[s - 1 - i]){ rem -= fact[s - 1 - i]; continue; } p[i] = j; dfs(i + 1, S | (1 << j), rem); break; } }; dfs(0, 0, n); ll ans = 0; for(int i = 0; i < s; i++)p2[p[i]] = i; for(int i = 0, S = 0; i < s; i++){ for(int j = 0; j < p2[i]; j++){ if(S >> j & 1)continue; ans += fact[s - 1 - i]; } S |= 1 << p2[i]; } cout << ans << endl; }