#include <bits/stdc++.h>
using namespace std;
long long f[30];
int gt(int l, long long n) {
  if (n >= l) return 0;
  int an = 0;
  an += gt(l, n * 10 + 4);
  an += gt(l, n * 10 + 7);
  return an + 1;
}
bool lk(int x) {
  bool ok = true;
  while (x) {
    int t = x % 10;
    if (t != 4 && t != 7) ok = false;
    x /= 10;
  }
  return ok;
}
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  f[0] = 1;
  int i = 1;
  while (true) {
    f[i] = f[i - 1] * i;
    if (f[i] >= k) break;
    ++i;
  }
  if (i > n) {
    printf("-1");
    return 0;
  }
  int s = n - i + 1;
  int an = gt(s, 4) + gt(s, 7);
  vector<int> p;
  for (int i = s; i < n + 1; ++i) p.push_back(i);
  for (int i = s; i < n + 1; ++i) {
    int j = n - i;
    int z, t;
    if (f[j] <= k) {
      z = (k - 1) / f[j];
      t = p[z];
      k = (k - 1) % f[j] + 1;
    } else
      z = 0, t = p[0];
    p.erase(p.begin() + z);
    if (lk(t) && lk(i)) ++an;
  }
  printf("%d", an);
}
