#include <bits/stdc++.h>
#define int long long
using namespace std;
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, k;
cin >> n >> k;
k--;
vector<int> ft(min(n+1, 20LL));
ft[0] = 1;
for(int i = 1; i < ft.size(); i++) {
ft[i] = ft[i-1] * i;
}
if(ft.back() <= k) {
cout << "-1\n";
return 0;
}
vector<int> a;
for(int i = ft.size()-1, j = k; i >= 1; i--) {
a.push_back(j / ft[i-1]);
j = j % ft[i-1];
}
vector<int> last;
for(int i = 0; i < a.size(); i++) {
last.push_back(n-a.size()+i+1);
}
for(int i = 0; i < last.size(); i++) {
swap(last[i], last[i+a[i]]);
sort(last.begin()+i+1, last.end());
}
function<int(int)> f = [&] (int i) {
if(i < n - last.size() + 1) {
return i;
}
else {
return last[i - (n - last.size() + 1)];
}
};
vector<int> lucky;
for(int d = 1; d <= 9; d++) {
for(int mask = 0; mask < (1 << d); mask++) {
int num = 0;
for(int i = 0; i < d; i++) {
if(mask & (1 << i)) {
num = num * 10 + 7;
}
else {
num = num * 10 + 4;
}
}
lucky.push_back(num);
}
}
sort(lucky.begin(), lucky.end());
int cnt = 0;
for(int i : lucky) {
if(i > n)
break;
int j = f(i);
if(binary_search(lucky.begin(), lucky.end(), j)) {
cnt++;
}
}
cout << cnt << "\n";
}
1555B - Two Tables | 1686A - Everything Everywhere All But One |
1469B - Red and Blue | 1257B - Magic Stick |
18C - Stripe | 1203B - Equal Rectangles |
1536A - Omkar and Bad Story | 1509A - Average Height |
1506C - Double-ended Strings | 340A - The Wall |
377A - Maze | 500A - New Year Transportation |
908D - New Year and Arbitrary Arrangement | 199A - Hexadecimal's theorem |
519C - A and B and Team Training | 631A - Interview |
961B - Lecture Sleep | 522A - Reposts |
1166D - Cute Sequences | 1176A - Divide it |
1527A - And Then There Were K | 1618E - Singers' Tour |
1560B - Who's Opposite | 182B - Vasya's Calendar |
934A - A Compatible Pair | 1618F - Reverse |
1684C - Column Swapping | 57C - Array |
1713D - Tournament Countdown | 33A - What is for dinner |