#include <bits/stdc++.h>
const int MAX_FACT = 14;
int N, K;
long long fact[MAX_FACT];
int countLucky(long long val, int n) {
  int sol = 0;
  if (val <= n) {
    if (val > 0) ++sol;
    sol += countLucky(val * 10 + 4, n);
    sol += countLucky(val * 10 + 7, n);
  }
  return sol;
}
bool isLucky(int val) {
  do {
    int digit = val % 10;
    if (digit != 4 && digit != 7) return false;
    val /= 10;
  } while (val > 0);
  return true;
}
int solve() {
  int sol = 0;
  fact[0] = 1;
  for (int i = 1; i < MAX_FACT; ++i) fact[i] = fact[i - 1] * i;
  int pos = 0;
  for (int i = 0; i < MAX_FACT; ++i)
    if (fact[i] >= K) {
      pos = i;
      break;
    }
  if (pos < N) sol += countLucky(0, N - pos);
  if (pos > N) return -1;
  int perm[MAX_FACT];
  bool fol[MAX_FACT];
  memset(fol, false, sizeof(fol));
  for (int i = 1; i <= pos; ++i) {
    long long cnt = 0;
    for (int j = 1; j <= pos; ++j)
      if (!fol[j]) {
        ++cnt;
        if (cnt * fact[pos - i] >= K) {
          fol[j] = true;
          perm[i] = N - pos + j;
          K -= (cnt - 1) * fact[pos - i];
          break;
        }
      }
  }
  for (int i = 1; i <= pos; ++i)
    if (isLucky(N - pos + i) && isLucky(perm[i])) ++sol;
  return sol;
}
int main() {
  assert(scanf("%d %d", &N, &K) == 2);
  printf("%d\n", solve());
}
