#include <bits/stdc++.h>
using namespace std;
const double dinf = 1e200;
void ext(int c) {}
int ar[30];
bool was[30];
int n;
int k;
long long fact[30];
vector<long long> nums;
void gen(long long v) {
  if (v > 1000000000) return;
  nums.push_back(v);
  gen(v * 10 + 4);
  gen(v * 10 + 7);
}
int main() {
  fact[0] = 1;
  for (int i = 0; i < (20); ++i) fact[i + 1] = fact[i] * (i + 1);
  cin >> n >> k;
  --k;
  if (n <= 13 && k >= fact[n]) {
    cout << -1;
    return 0;
  }
  int p = n + 1;
  while (k >= fact[n - p + 1]) --p;
  for (int a = p; a <= n; ++a) {
    for (int v = p;; ++v) {
      if (was[v - p]) continue;
      if (k < fact[n - a]) {
        was[v - p] = true;
        ar[a - p] = v;
        break;
      }
      k -= fact[n - a];
    }
  }
  gen(4);
  gen(7);
  sort(nums.begin(), nums.end());
  long long res = lower_bound(nums.begin(), nums.end(), p) - nums.begin();
  for (int a = p; a <= n; ++a) {
    int i1 = lower_bound(nums.begin(), nums.end(), a) - nums.begin();
    int i2 = lower_bound(nums.begin(), nums.end(), ar[a - p]) - nums.begin();
    if (i1 == nums.size() || i2 == nums.size() || nums[i1] != a ||
        nums[i2] != ar[a - p])
      continue;
    ++res;
  }
  cout << res;
  return 0;
}
