#include <bits/stdc++.h>
using namespace std;
long long n, k, f[25];
vector<long long> v, sol;
set<long long> s;
void dfs(long long now) {
  if (now > 1000000000) return;
  v.push_back(now);
  s.insert(now);
  dfs(now * 10 + 4);
  dfs(now * 10 + 7);
}
void build() {
  dfs(4);
  dfs(7);
  sort(v.begin(), v.end());
  f[0] = 1;
  for (int i = 1; i < 20; i++) f[i] = i * f[i - 1];
}
bool che(long long now) { return s.count(now); }
long long getnum(int now) {
  long long cnt = 0, num = 0;
  for (int i = 0; i < sol.size(); i++) {
    if (sol[i] != -1000000000) cnt++;
    if (cnt == now) {
      num = sol[i];
      sol[i] = -1000000000;
      break;
    }
  }
  return num;
}
int main() {
  build();
  cin >> n >> k;
  if (n <= 15 && k > f[n]) {
    cout << -1 << '\n';
    return 0;
  }
  long long pos = n - 14, ans = 0;
  for (auto i : v) {
    if (i < pos) ans++;
  }
  for (int i = 14; i >= 0; i--) sol.push_back(n - i);
  for (int i = 15; i >= 1; i--) {
    int cnt = 1;
    while (k > f[i - 1]) {
      k -= f[i - 1];
      cnt++;
    }
    long long now = getnum(cnt);
    if (che(pos) && che(now)) ans++;
    pos++;
  }
  cout << ans << '\n';
  return 0;
}
