#include <bits/stdc++.h>
using namespace std;
long long fact[20];
int offset[20];
int vals[20];
vector<int> lidx;
bool islucky(int x) {
  while (x > 0) {
    if (x % 10 != 4 && x % 10 != 7) return false;
    x /= 10;
  }
  return true;
}
int main() {
  fact[0] = fact[1] = 1;
  for (int i = 2; i < 20; i++) fact[i] = (fact[i - 1] * i);
  int N, K;
  cin >> N >> K;
  K -= 1;
  for (int i = min(13, N - 1); i >= 0; i--) {
    offset[i] = K / fact[i];
    if (offset[i] > i) {
      cout << -1 << endl;
      return 0;
    }
    K -= fact[i] * offset[i];
  }
  set<int> rem;
  for (int i = max(1, N - 13); i <= N; i++) rem.insert(i);
  for (int i = min(13, N - 1); i >= 0; i--) {
    auto it = rem.begin();
    for (int j = 0; j < offset[i]; j++, it++)
      ;
    vals[i] = (*it);
    rem.erase(it);
  }
  queue<long long> q;
  q.push(4);
  q.push(7);
  while (!q.empty()) {
    long long x = q.front();
    q.pop();
    if (x > N)
      continue;
    else
      lidx.push_back(x);
    q.push(x * 10 + 4);
    q.push(x * 10 + 7);
  }
  sort(lidx.begin(), lidx.end());
  int total = 0;
  for (auto x : lidx)
    if (N - x > 13) total += 1;
  for (int i = min(13, N - 1), j = max(N - 13, 1); i >= 0; i--, j++)
    if (islucky(j) && islucky(vals[i])) total += 1;
  cout << total << endl;
  return 0;
}
