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