#include <bits/stdc++.h>
using namespace std;
long long numlucky(long long i, long long bound) {
  if (i > bound)
    return 0;
  else
    return 1 + numlucky(i * 10 + 4, bound) + numlucky(i * 10 + 7, bound);
}
bool check(long long i) {
  if (i <= 0) return false;
  while (i > 0) {
    int a = i % 10;
    if (a != 4 && a != 7) return false;
    i = i / 10;
  }
  return true;
}
int main() {
  std::ios_base::sync_with_stdio(false);
  long long n, k;
  cin >> n >> k;
  long long fact[14];
  fact[1] = 1;
  for (long long i = 2; i <= 13; i++) fact[i] = i * fact[i - 1];
  if (n < 14 && fact[n] < k) {
    cout << -1 << endl;
    return 0;
  }
  long long num = numlucky(0, max((long long)0, n - 13)) - 1;
  int t = 1;
  k -= 1;
  vector<int> perm;
  for (int i = n - 12; i <= n; i++) perm.push_back(i);
  for (int i = 13; i >= 2; i--) {
    int index = k / fact[i - 1];
    if (check(perm[index]) && check(n - i + 1)) num++;
    perm.erase(perm.begin() + index);
    k = k % fact[i - 1];
  }
  if (check(perm[0]) && check(n)) num++;
  cout << num << endl;
  return 0;
}
