#include <bits/stdc++.h>
using namespace std;
int n, k, f[13], p[13];
vector<int> lk;
map<int, bool> bea;
void gen(int x) {
  if (x) lk.push_back(x), bea[x] = true;
  if (x > int(1e8)) return;
  gen(x * 10 + 4);
  gen(x * 10 + 7);
}
int main() {
  scanf("%d%d", &n, &k);
  f[0] = 1;
  for (int i = 1; i <= 12; ++i) f[i] = f[i - 1] * i;
  if (n <= 12 && k > f[n]) {
    printf("-1");
    return 0;
  }
  gen(0);
  lk.push_back(int(1e9) + 1);
  sort(lk.begin(), lk.end());
  int d;
  for (int i = 1; i <= n; ++i)
    if (f[i] >= k) {
      d = i;
      break;
    }
  int ans = 0;
  for (int i = 0; i < lk.size(); ++i)
    if (lk[i] > n - d) {
      ans = i;
      break;
    }
  for (int i = 1; i <= d; ++i) {
    for (int j = 1; j <= d; ++j)
      if (!p[j]) {
        if (k > f[d - i])
          k -= f[d - i];
        else {
          p[j] = 1;
          if (bea[n - d + i] && bea[n - d + j]) ++ans;
          break;
        }
      }
  }
  printf("%d", ans);
  return 0;
}
