#include <bits/stdc++.h>
using namespace std;
template <class T>
T abs(T x) {
  return x > 0 ? x : (-x);
}
template <class T>
T sqr(T x) {
  return x * x;
}
vector<long long> gen() {
  vector<long long> res;
  for (int len = 1; len <= 11; ++len)
    for (int mask = 0; mask < (1 << len); ++mask) {
      long long cur = 0;
      for (int i = 0; i < len; ++i)
        cur = cur * 10LL + (long long)(((mask >> i) & 1) ? 7 : 4);
      res.push_back(cur);
    }
  sort((res).begin(), (res).end());
  return res;
}
long long f[40];
vector<int> solve(int n, int k) {
  --k;
  f[0] = 1;
  for (int i = 1; i < 40; ++i) f[i] = f[i - 1] * (long long)i;
  if (k >= f[n]) {
    printf("-1\n");
    exit(0);
  }
  vector<int> res;
  vector<int> u(n, 0);
  for (int i = 0; i < n; ++i) {
    int cur = 0;
    while (k >= f[n - i - 1]) ++cur, k -= f[n - i - 1];
    int x = 0;
    while (cur > 0) {
      if (!u[x]) --cur;
      ++x;
    }
    while (u[x]) ++x;
    u[x] = true;
    res.push_back(x);
  }
  return res;
}
int main() {
  vector<long long> v = gen();
  int n, k;
  cin >> n >> k;
  int res = 0;
  for (int i = 0; i < ((int)(v).size()); ++i)
    if (v[i] < n - 14) ++res;
  vector<int> w;
  for (int i = max(n - 14, 1); i <= n; ++i) w.push_back(i);
  vector<int> p = solve(((int)(w).size()), k);
  vector<int> ww(((int)(w).size()));
  for (int i = 0; i < ((int)(w).size()); ++i) ww[i] = w[p[i]];
  for (int i = 0; i < ((int)(w).size()); ++i) {
    int x = max(n - 14, 1) + i;
    if (!binary_search((v).begin(), (v).end(), (long long)x)) continue;
    if (!binary_search((v).begin(), (v).end(), (long long)ww[i])) continue;
    ++res;
  }
  cout << res << "\n";
  return 0;
}
