#include <bits/stdc++.h>
using namespace std;
int logfac(long long k) {
  long long fac = 1;
  int ans = 0;
  while (fac < k) {
    ans++;
    fac *= ans;
  }
  return ans;
}
int a[100];
long long fact[30];
bool mark[100];
void make_permutation(int now, int n, int k) {
  if (now == n) return;
  int i;
  for (i = 1; i <= n; i++)
    if (!mark[i]) break;
  while (fact[n - now - 1] < k) {
    k -= fact[n - now - 1];
    i++;
    while (mark[i]) i++;
  }
  a[now] = i;
  mark[i] = true;
  make_permutation(now + 1, n, k);
}
bool lucky(long long n) {
  while (n != 0) {
    if (n % 10 != 4 && n % 10 != 7) return false;
    n /= 10;
  }
  return true;
}
void addLuckeys(long long now, long long Max, int &ans) {
  if (now > Max) return;
  if (now != 0) ans++;
  addLuckeys(now * 10 + 4, Max, ans);
  addLuckeys(now * 10 + 7, Max, ans);
}
int main() {
  int n, k;
  cin >> n >> k;
  int changable = logfac(k);
  if (changable > n) {
    cout << -1 << endl;
    return 0;
  }
  fact[0] = 1;
  for (int i = 1; fact[i - 1] < 1e9; i++) fact[i] = fact[i - 1] * i;
  make_permutation(0, changable, k);
  int ans = 0;
  for (int i = 0; i < changable; i++) {
    if (lucky(n - changable + 1 + i) && lucky(a[i] + n - changable)) ans++;
  }
  addLuckeys(0, n - changable, ans);
  cout << ans << endl;
  return 0;
}
