#include <bits/stdc++.h>
using namespace std;
int n, k, l, a[2000], mk[15], s[15];
long long f[15];
map<int, bool> lc;
void F(long long x) {
  if (x > 1e9) return;
  if (x > 0) {
    a[++l] = x;
    lc[x] = 1;
  }
  F(x * 10LL + 4LL);
  F(x * 10LL + 7LL);
}
int main() {
  f[0] = 1LL;
  for (int i = 1; i <= 13; i++) f[i] = f[i - 1] * 1LL * i;
  scanf("%d%d", &n, &k);
  if (n <= 12 && k > f[n]) {
    printf("-1\n");
    return 0;
  }
  int t = 1;
  while (f[t] < k) t++;
  F(0);
  sort(a + 1, a + l + 1);
  int sol = 0;
  for (int i = 1; i <= l; i++) {
    if (a[i] > n - t) break;
    sol++;
  }
  for (int i = 1; i <= t; i++) {
    for (int j = 1; j <= t; j++) {
      if (mk[j]) continue;
      if (k > f[t - i]) {
        k -= f[t - i];
        continue;
      }
      s[i] = j + n - t;
      mk[j] = 1;
      break;
    }
  }
  for (int i = 1; i <= t; i++)
    if (lc.count(i + n - t) && lc.count(s[i])) sol++;
  printf("%d\n", sol);
  return 0;
}
