#include <bits/stdc++.h>
using namespace std;
int findFirstNumIndex(int& k, int n) {
  if (n == 1) return 0;
  n--;
  int first_num_index;
  int n_partial_fact = n;
  while (k >= n_partial_fact && n > 1) {
    n_partial_fact = n_partial_fact * (n - 1);
    n--;
  }
  first_num_index = k / n_partial_fact;
  k = k % n_partial_fact;
  return first_num_index;
}
vector<int> findKthPermutation(int n, int k) {
  vector<int> ans;
  set<int> s;
  for (int i = 1; i <= n; i++) s.insert(i);
  set<int>::iterator itr;
  itr = s.begin();
  k = k - 1;
  for (int i = 0; i < n; i++) {
    int index = findFirstNumIndex(k, n - i);
    advance(itr, index);
    ans.push_back((*itr));
    s.erase(itr);
    itr = s.begin();
  }
  return ans;
}
bool islucky(int n) {
  while (n > 0) {
    if ((n % 10 != 4) && (n % 10 != 7)) return false;
    n /= 10;
  }
  return true;
}
int getans(long long x, int n) {
  if (x > n) return 0;
  return getans(x * 10LL + 4LL, n) + getans(x * 10LL + 7LL, n) + (x != 0);
}
int main() {
  int n, k;
  cin >> n >> k;
  vector<long long> fact(14);
  fact[0] = 1;
  for (long long i = 1; i < 14; i++) {
    fact[i] = fact[i - 1] * i;
  }
  if (n < 14 && fact[n] < k) {
    cout << -1 << endl;
    return 0;
  }
  int y = min(13, n);
  vector<int> ans = findKthPermutation(y, k);
  long long cnt = 0;
  map<int, int> mm;
  for (int i = n, j = y; i > n - y; --i, --j) {
    mm[j] = i;
  }
  for (int i = 0; i < ans.size(); i++) {
    ans[i] = mm[ans[i]];
  }
  cnt += (getans(0, n - y));
  for (int x = n - y + 1, i = 0; x <= n; ++x, ++i) {
    cnt += (islucky(x) && islucky(ans[i]));
  }
  cout << cnt << endl;
  return 0;
}
