#include <bits/stdc++.h>
using namespace std;
long long int mul[1000];
int i, j, n, m, t, tt, k;
void prepare() {
  mul[0] = 1;
  i = 0;
  while (mul[i] < 2000000000) {
    mul[i + 1] = (i + 1) * mul[i];
    i++;
  }
  tt = i;
}
void kper(int p[1000], int n, long long int k) {
  bool checked[1000];
  memset(checked, 0, sizeof(checked));
  int i, j;
  for (i = 1; i <= n; i++) {
    j = 1;
    while (1) {
      while (checked[j]) j++;
      if (mul[n - i] >= k) {
        break;
      }
      k -= mul[n - i];
      j++;
    }
    checked[j] = 1;
    p[i] = j;
  }
}
long long int p[20000];
void prepare2() {
  p[1] = 4;
  p[2] = 7;
  ;
  t = 2;
  j = 1;
  int r;
  for (i = 1; i <= 10; i++) {
    r = t;
    while (j <= r) {
      p[++t] = p[j] * 10 + 4;
      p[++t] = p[j] * 10 + 7;
      j++;
    }
  }
  sort(&p[1], &p[1 + t]);
}
bool lucky(long long int a) {
  while (a) {
    if (a % 10 != 4 && a % 10 != 7) return 0;
    a /= 10;
  }
  return 1;
}
int q[1000], qq[1000], qqq[1000];
int main() {
  prepare();
  prepare2();
  scanf("%d %d", &n, &k);
  if (n <= tt && mul[n] < k) {
    printf("-1\n");
    return 0;
  }
  i = 1;
  while (mul[i] < k) i++;
  j = n - i;
  if (j < 1) j = 1;
  int a, b, c, ans = 0;
  a = 1;
  while (p[a] <= j) {
    ans++;
    a++;
  }
  kper(q, i, k);
  for (j = n; j > n - i; j--) {
    qq[j - (n - i)] = j;
  }
  for (j = 1; j <= i; j++) {
    qqq[j] = qq[q[j]];
  }
  for (j = 1; j <= i; j++) {
    if (lucky(qqq[j]) && lucky(j + (n - i))) {
      ans++;
    }
  }
  printf("%d\n", ans);
  return 0;
}
