#include <bits/stdc++.h>
using namespace std;
int n, k, N;
int fact[13], p[13], a[13];
int first(int n) {
  queue<long long> q;
  q.push(0);
  int res = -1;
  while (q.front() <= 1LL * n) {
    long long cur = q.front();
    res++;
    q.pop();
    q.push(10 * cur + 4);
    q.push(10 * cur + 7);
  }
  return res;
}
int ok(int x) {
  while (x) {
    if (!(x % 10 == 4 || x % 10 == 7)) {
      return 0;
    }
    x /= 10;
  }
  return 1;
}
int main() {
  fact[0] = 1;
  for (int i = 1; i <= 12; i++) {
    fact[i] = fact[i - 1] * i;
    a[i] = i;
  }
  a[13] = 13;
  scanf("%d", &n);
  scanf("%d", &k);
  if (n < 13 && fact[n] < k) {
    puts("-1");
    return 0;
  }
  N = min(n, 13);
  k--;
  for (int i = 1; i <= N; i++) {
    int l = k / fact[N - i];
    k %= fact[N - i];
    p[i] = a[l + 1];
    for (int j = l + 2; j <= N; j++) a[j - 1] = a[j];
  }
  int ans = 0;
  if (n <= 13) {
    for (int i = 1; i <= n; i++) {
      if ((i == 4 || i == 7) && (p[i] == 4 || p[i] == 7)) ans++;
    }
  } else {
    for (int i = n; i > n - 13; i--) {
      if (ok(i) && ok(p[i - n + 13] + n - 13)) {
        ans++;
      }
    }
    ans = ans + first(n - 13);
  }
  printf("%d\n", ans);
  return 0;
}
