#include <bits/stdc++.h>
using namespace std;
int luck[5000], top;
int a[20], fa[20] = {1};
bool used[20];
void make(long long x) {
  if (x > 1e9) return;
  luck[top++] = x;
  make(x * 10 + 4);
  make(x * 10 + 7);
}
bool is(int x) { return x == 0 || (x % 10 == 4 || x % 10 == 7) && is(x / 10); }
void inttoarray(int k, int n) {
  int i, j, tmp;
  for (i = 1; i <= n; i++) {
    tmp = k / fa[n - i];
    for (j = 1; j <= n; j++)
      if (!used[j]) {
        if (tmp == 0) break;
        tmp--;
      }
    a[i] = j;
    used[j] = 1;
    k %= fa[n - i];
  }
}
int main() {
  int n, k, i, j, as = 0;
  for (i = 1; i < 20; i++) fa[i] = fa[i - 1] * i;
  scanf("%d%d", &n, &k);
  k--;
  if (n <= 13) {
    if (k >= fa[n]) return puts("-1");
    inttoarray(k, n);
    if (a[4] == 4 || a[4] == 7) as++;
    if (a[7] == 4 || a[7] == 7) as++;
    printf("%d\n", as);
    return 0;
  }
  make(0);
  sort(luck + 1, luck + top);
  inttoarray(k, 13);
  for (i = 1; i <= 13; i++)
    if (is(i + n - 13) && is(a[i] + n - 13)) as++;
  for (i = 1; i < top; i++)
    if (luck[i] > n - 13) break;
  as += i - 1;
  printf("%d\n", as);
  return 0;
}
