#include <bits/stdc++.h>
using namespace std;
vector<long long> lucky;
long long l, r;
void gen(long long x) {
  if (x >= 4444444444ll) return;
  lucky.push_back(x);
  gen(x * 10 + 4);
  gen(x * 10 + 7);
}
bool islucky(int x) {
  if (!x) return (false);
  while (x) {
    if (x % 10 != 4 && x % 10 != 7) return (false);
    x /= 10;
  }
  return (true);
}
int main() {
  int n, k, r;
  long long x = 1;
  scanf("%d %d", &n, &k);
  bool ok = false;
  for (int i = 1; i <= n; i++) {
    x *= i;
    if (x >= k) {
      ok = true;
      r = i;
      break;
    }
  }
  if (!ok) return (!printf("-1\n"));
  gen(0);
  sort(lucky.begin(), lucky.end());
  int res = 0;
  for (int i = 0; i < lucky.size(); i++) {
    if (lucky[i] && lucky[i] <= n - r) res++;
  }
  bool used[128];
  int num[128];
  memset(used, false, sizeof(used));
  for (int i = 1; i <= r; i++) {
    int t = 1;
    x = 1;
    for (int m = 1; m <= r - i; m++) x *= m;
    while (t * x < k) t++;
    int cnt = 0;
    for (int j = 1; j <= r; j++) {
      if (!used[j]) cnt++;
      if (cnt == t && !used[j]) {
        num[i] = j;
        used[j] = 1;
        break;
      }
    }
    k -= (t - 1) * x;
    if (k <= 0) k = 1;
  }
  for (int i = 1; i <= r; i++) {
    if (islucky(num[i] + n - r) && islucky(i + n - r)) res++;
  }
  printf("%d\n", res);
  return (0);
}
