#include <bits/stdc++.h>
using namespace std;
int getall(long long x, int n) {
  if (x > n) return 0;
  return 1 + getall(x * 10 + 4, n) + getall(x * 10 + 7, n);
}
bool is_good(int x) {
  if (!x) return 0;
  while (x) {
    if (x % 10 != 4 && x % 10 != 7) return 0;
    x /= 10;
  }
  return 1;
}
vector<int> kth(int n, int k) {
  vector<long long> f(n);
  f[0] = 1;
  for (auto i = 1; i < n; i++) f[i] = i * f[i - 1];
  vector<int> perm(n), u(n, 0);
  for (auto i = 0; i < n; i++) {
    for (auto j = 0; j < n; j++) {
      if (u[j]) continue;
      long long add = f[n - 1 - i];
      if (add < k) {
        k -= add;
        continue;
      }
      perm[i] = j;
      break;
    }
    u[perm[i]] = 1;
  }
  return perm;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  long long fac = 1;
  int cnt;
  for (auto i = 1; i < n + 1; i++) {
    fac *= i;
    cnt = i;
    if (fac >= k) break;
  }
  if (fac < k) {
    cout << -1;
    return 0;
  }
  int tot = getall(0, n) - 1;
  vector<int> perm = kth(cnt, k);
  for (auto i = 0; i < cnt; i++) {
    int pos = n - cnt + 1 + i, val = perm[i] + n - cnt + 1;
    if (is_good(val) && !is_good(pos)) --tot;
  }
  cout << tot;
}
