#include <bits/stdc++.h>   using namespace std;   vector<long long> v, ext;   long long f[20];   bool vis[20];   bool chck(long long j) {     while (j) {       if (j % 10 != 4 && j % 10 != 7) break;       j /= 10;     }     return j == 0;   }   int main() {     long long i, n, k, st, sum, j, pre, sz, ans;     f[0] = 1;     for (i = 1; i <= 13; i++) f[i] = f[i - 1] * i;     cin >> n >> k;     if (n <= 13 && k > f[n]) {       cout << -1 << endl;       return 0;     }     for (i = 1; f[i] < k; i++)       ;     st = n - i + 1;     sum = k;     for (j = 1; j <= i; j++) {       for (k = st; k <= n; k++) {         if (!vis[k - st]) {           if (sum - f[i - j] > 0) {             sum -= f[i - j];           } else {             ext.push_back(k);             vis[k - st] = 1;             break;           }         }       }     }     queue<long long> q;     q.push(4);     q.push(7);     v.push_back(4);     v.push_back(7);     while (!q.empty()) {       pre = q.front();       q.pop();       if (pre * 10 + 4 <= 5000000000LL) {         q.push(pre * 10 + 4);         q.push(pre * 10 + 7);         v.push_back(pre * 10 + 4);         v.push_back(pre * 10 + 7);       }     }     sz = v.size();     ans = 0;     for (i = 0; i < sz && v[i] < st; i++) ans++;     for (i = st; i <= n; i++) {       if (chck(i)) {         if (chck(ext[i - st])) ans++;       }     }     cout << ans << endl;     return 0;   }   