#include <bits/stdc++.h>
using namespace std;
const int maxn = 10000 + 1000;
const int inf = 1e9;
int per[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
bool is_lucky(int x) {
  while (x) {
    if (x % 10 != 4 && x % 10 != 7) return 0;
    x /= 10;
  }
  return 1;
}
long long makelucky(long long x, int l) {
  long long lucky = 0;
  while (l--) {
    if (x & 1)
      lucky = lucky * 10 + 7;
    else
      lucky = lucky * 10 + 4;
    x /= 2;
  }
  return lucky;
}
long long solve(int n) {
  set<long long> st;
  for (long long l = 1; l <= 10; l++)
    for (long long i = 0; i < ((long long)1 << l); i++)
      st.insert(makelucky(i, l));
  long long res = 0;
  while (!st.empty()) {
    long long lucky = *st.begin();
    st.erase(*st.begin());
    res += (lucky <= n - 13);
  }
  return res;
}
long long fact(long long x) {
  long long res = 1;
  for (int i = 1; i <= x; i++) res *= i;
  return res;
}
void kthper(int k, int l, int r) {
  if (r - l == 0) return;
  long long f = fact(r - l), t = 0;
  while ((t + 1) * f <= k) t++;
  if (t) {
    int b = per[l];
    per[l] = per[l + t];
    for (int i = l + t; i >= l + 2; i--) per[i] = per[i - 1];
    per[l + 1] = b;
  }
  kthper(k - t * f, l + 1, r);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, k;
  cin >> n >> k;
  if (n <= 14) {
    long long res = 1;
    for (int i = 1; i <= n; i++) res *= i;
    if (k > res) return cout << -1 << endl, 0;
  }
  int r = min(n, (long long)13);
  long long ans = solve(n);
  kthper(k - 1, 1, r);
  for (int i = 1; i <= r; i++)
    ans += (is_lucky(n - r + i) && is_lucky(per[i] + n - r));
  cout << ans << endl;
  return 0;
}
