#include <bits/stdc++.h>
using namespace std;
const int N = 1 << 20;
long long fac[20];
vector<long long> lst;
vector<long long> lucky;
void prepare(int cnt, int all, long long now) {
  if (cnt == all) {
    lucky.push_back(now);
    return;
  }
  prepare(cnt + 1, all, now * 10 + 4);
  prepare(cnt + 1, all, now * 10 + 7);
}
inline bool chk(long long x) {
  while (x) {
    if (x % 10 != 4 && x % 10 != 7) return false;
    x /= 10;
  }
  return true;
}
int main() {
  ios::sync_with_stdio(false);
  for (int i = 0; i <= 10; i++) prepare(0, i, 0);
  fac[0] = 1;
  for (int i = 1; i < 20; i++) fac[i] = fac[i - 1] * i;
  long long n, k;
  cin >> n >> k;
  long long p = 1;
  while (fac[p] < k) p++;
  if (p > n) return cout << -1 << '\n', 0;
  k--;
  for (long long i = n - p + 1; i <= n; i++) lst.push_back(i);
  while (k) {
    int i;
    for (i = p; ~i; i--)
      if (fac[i] <= k) break;
    int w = k / fac[i];
    k -= fac[i] * w;
    for (int s = 0; s < w; s++) swap(lst[p - 1 - i], lst[p - i + s]);
  }
  long long res =
      upper_bound(lucky.begin(), lucky.end(), n - p) - lucky.begin() - 1;
  for (int i = 0; i < lst.size(); i++)
    if (chk(n - p + i + 1) && chk(lst[i])) res++;
  cout << res << '\n';
  return 0;
}
