#include <bits/stdc++.h>
using namespace std;
long long lucky[1024 * 4];
long long fact[14];
int cnt;
void func(long long cur, int pos, int maxpos) {
  if (pos == maxpos) {
    lucky[++cnt] = cur * 10 + 4;
    lucky[++cnt] = cur * 10 + 7;
    return;
  }
  func(cur * 10 + 4, pos + 1, maxpos);
  func(cur * 10 + 7, pos + 1, maxpos);
}
void pre() {
  cnt = 0;
  for (int d = 1; d <= 10; d++) {
    func(0, 1, d);
  }
  sort(lucky, lucky + cnt);
  fact[0] = 1;
  for (int i = 1; i < 14; i++) fact[i] = i * fact[i - 1];
}
long long n, k;
void in() { cin >> n >> k; }
int arr[14] = {0};
int brr[14] = {0};
void rec(int p, long long k, int coun) {
  if (coun == p) return;
  int b = (k + fact[p - 1 - coun] - 1) / fact[p - 1 - coun];
  int c = 0;
  for (int i = 0; i < 14; i++) {
    if (brr[i] == 0) c++;
    if (b == c) {
      brr[i] = 1;
      arr[coun] = i;
      rec(p, k - fact[p - 1 - coun] * (b - 1), coun + 1);
      break;
    }
  }
  return;
}
bool luck(long long q) {
  for (int i = 0; i < cnt; i++) {
    if (lucky[i] == q) return true;
    if (lucky[i] > q) return false;
  }
  return false;
}
void solve() {
  int p = 0;
  while (fact[p] < k) p++;
  if (n < 14 && k > fact[n]) {
    cout << -1 << endl;
    return;
  }
  rec(p, k, 0);
  for (int i = 0; i < p; i++) {
    arr[i] += n - p + 1;
  }
  int ans = 1;
  while (lucky[ans] <= n - p) {
    ans++;
  }
  ans--;
  for (long long i = n - p + 1; i <= n; i++) {
    if (luck(i) && luck(arr[i - n - 1 + p])) ans++;
  }
  cout << ans << endl;
}
int main() {
  pre();
  in();
  solve();
}
