#include <bits/stdc++.h>
using namespace std;
const int MAXN = -1;
long long n, K, pot[20], fact[20], ans;
set<long long> luckySet;
bool used[20];
long long arr[20];
void kth(int pos, int size, long long k) {
  if (pos == size + 1) {
    return;
  }
  long long p = 1;
  while (true) {
    assert(p <= 20);
    if (used[p]) {
      p++;
      continue;
    }
    if (k > fact[size - pos]) {
      k -= fact[size - pos];
      p++;
      continue;
    }
    break;
  }
  arr[pos - 1] = p;
  used[p] = true;
  kth(pos + 1, size, k);
}
bool isLucky(long long v, const vector<long long> &lucky) {
  for (int i = (0); i < (int)(lucky.size()); i++) {
    if (lucky[i] == v) return true;
  }
  return false;
}
int main() {
  scanf("%lld %lld", &n, &K);
  pot[0] = 1;
  for (int i = (1); i < (int)(18); i++) {
    pot[i] = pot[i - 1] * 10LL;
  }
  fact[0] = 1;
  for (int i = (1); i < (int)(20); i++) {
    fact[i] = fact[i - 1] * i;
  }
  if (n <= 15 && fact[n] < K) {
    printf("-1\n");
    return 0;
  }
  for (int bm = (0); bm < (int)(1024); bm++) {
    for (int j = (1); j < (int)(11); j++) {
      long long l = 0;
      for (int k = (0); k < (int)(j); k++) {
        l += (bm & (1 << k)) ? 7 * pot[k] : 4 * pot[k];
      }
      luckySet.insert(l);
    }
  }
  vector<long long> lucky(luckySet.begin(), luckySet.end());
  for (int i = (0); i < (int)(lucky.size()); i++) {
    if (lucky[i] <= n - 15) {
      ans++;
    }
  }
  int sz = min(n, 15LL);
  kth(1, sz, K);
  for (int i = (0); i < (int)(sz); i++) {
    arr[i] += n - sz;
    if (isLucky(n - sz + 1 + i, lucky) && isLucky(arr[i], lucky)) {
      ans++;
    }
  }
  printf("%lld\n", ans);
  return 0;
}
