#include <bits/stdc++.h>
using namespace std;
long long fact[20];
int permutation[20];
vector<int> canUse;
int luckySmaller(int n) {
  queue<long long> q;
  while (!q.empty()) q.pop();
  q.push(0);
  int ans = 0;
  while (!q.empty()) {
    long long f = q.front();
    q.pop();
    long long a[2] = {f * 10 + 4, f * 10 + 7};
    for (int i = 0; i < 2; i++)
      if (a[i] < n) {
        q.push(a[i]);
        ans++;
      }
  }
  return ans;
}
void build(int n, int k, int idx) {
  if (n == 0) return;
  if (n == 1) {
    permutation[idx] = canUse[0];
    return;
  }
  int use = k / fact[n - 1];
  permutation[idx] = canUse[use];
  swap(canUse[use], canUse[canUse.size() - 1]);
  canUse.pop_back();
  sort(canUse.begin(), canUse.end());
  build(n - 1, k % fact[n - 1], idx + 1);
}
bool isLucky(int n) {
  while (n != 0) {
    int d = n % 10;
    if (d != 4 && d != 7) return false;
    n /= 10;
  }
  return true;
}
int main() {
  int n, k;
  cin >> n >> k;
  int start = 0;
  fact[0] = 1;
  while (fact[start] < k) {
    start++;
    fact[start] = fact[start - 1] * start;
    if (start > n) {
      cout << -1 << endl;
      return 0;
    }
  }
  for (int i = n - start + 1; i <= n; i++) canUse.push_back(i);
  build(start, k - 1, 0);
  int ans = luckySmaller(n - start + 1);
  for (int i = 0; i < start; i++) {
    int idx = n - start + 1 + i;
    if (isLucky(permutation[i]) && isLucky(idx)) ans++;
  }
  cout << ans << endl;
}
