#include <bits/stdc++.h>
using namespace std;
bool islucky(int x) {
  while (x) {
    if (x % 10 != 4 && x % 10 != 7) return false;
    x /= 10;
  }
  return true;
}
int main() {
  int N, K;
  scanf("%d%d", &N, &K);
  vector<long long> u;
  for (int(i) = 0; (i) < (int)(11); ++(i))
    if (i)
      for (int(mask) = 0; (mask) < (int)(1 << i); ++(mask)) {
        long long p = 0;
        for (int(j) = 0; (j) < (int)(i); ++(j)) {
          if (mask >> j & 1)
            p = p * 10 + 7;
          else
            p = p * 10 + 4;
        }
        u.push_back(p);
      }
  sort((u).begin(), (u).end());
  int n = min(N, 13);
  vector<long long> f(20, 1);
  for (int(i) = (int)(1); (i) <= (int)(19); ++(i)) f[i] = f[i - 1] * 1ll * i;
  if (N < 13 && K > f[n]) return puts("-1");
  int k = K - 1;
  vector<bool> vis(n, false);
  vector<int> v;
  for (int(i) = 0; (i) < (int)(n); ++(i))
    for (int(j) = 0; (j) < (int)(n); ++(j))
      if (!vis[j]) {
        if (k < f[n - 1 - i]) {
          v.push_back(j);
          vis[j] = true;
          break;
        } else
          k -= f[n - 1 - i];
      }
  int ans = 0, m = N - n + 1;
  for (int(i) = 0; (i) < (int)(n); ++(i))
    if (islucky(m + i) && islucky(m + v[i])) ans++;
  for (int(i) = 0; (i) < (int)(u.size()); ++(i))
    if (u[i] < m) ans++;
  printf("%d\n", ans);
}
