#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-6;
const int N = 2000010;
const int INF = 0x3f3f3f3f;
const int mod = 1000000007;
const int dir[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, -1, -1, -1, 1, 1, -1, 1, 1};
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
template <class T>
bool ckmin(T& a, const T& b) {
  return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
  return a < b ? a = b, 1 : 0;
}
template <class T>
void read(T& x) {
  char c;
  bool op = 0;
  while (c = getchar(), c < '0' || c > '9')
    if (c == '-') op = 1;
  x = c - '0';
  while (c = getchar(), c >= '0' && c <= '9') x = x * 10 + c - '0';
  if (op) x = -x;
}
template <class T>
void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x > 9) write(x / 10);
  putchar(x % 10 + 48);
}
long long n, k;
long long ans;
long long fac[N] = {1};
long long res[N];
long long tot = 0;
long long find(long long x) {
  sort(res + 1, res + 1 + tot);
  long long ret = res[x];
  for (int i = x + 1; i <= tot; i++) {
    res[i - 1] = res[i];
  }
  --tot;
  return ret;
}
void dfs(long long x, long long lim) {
  if (x > lim) return;
  if (x != 0) ++ans;
  dfs(x * 10 + 4, lim);
  dfs(x * 10 + 7, lim);
}
bool ck(long long x) {
  while (x) {
    if (x % 10 != 4 && x % 10 != 7) return false;
    x /= 10;
  }
  return true;
}
void solve() {
  read(n), read(k);
  long long p = -1;
  for (int i = 1; i <= n; i++) {
    fac[i] = fac[i - 1] * i;
    res[++tot] = n - i + 1;
    if (fac[i] >= k) {
      p = i;
      break;
    }
  }
  k--;
  if (p == -1) {
    puts("-1");
    return;
  }
  dfs(0, n - p);
  for (long long i = p, val, pos; i >= 1; i--) {
    val = find(k / fac[i - 1] + 1);
    pos = n - i + 1;
    if (ck(val) && ck(pos)) {
      ++ans;
    }
    k = k % fac[i - 1];
  }
  write(ans);
  putchar('\n');
}
int main() {
  solve();
  return 0;
}
