#include <bits/stdc++.h>
using namespace std;
const int N = 210;
const int M = 200000;
const int K = 510;
const long long LIT = 1000000000LL;
const int INF = 1 << 30;
long long n, k;
vector<long long> num;
void dfs(long long u) {
  if (u) num.push_back(u);
  if (u > LIT) return;
  dfs(u * 10 + 4);
  dfs(u * 10 + 7);
}
void init() {
  dfs(0);
  sort(num.begin(), num.end());
}
bool init2() {
  long long tmp = 1;
  for (long long i = 1; i <= n; i++) {
    tmp *= i;
    if (tmp >= k) return 1;
  }
  cout << -1 << endl;
  return 0;
}
bool valid(int x) {
  for (int i = 0; i < num.size(); i++)
    if (num[i] == x) return 1;
  return 0;
}
vector<long long> cal(vector<long long> v, long long m) {
  int len = v.size();
  vector<long long> ret = vector<long long>(len);
  for (int i = len - 1; i >= 0; i--) {
    ret[i] = m % (len - i);
    m /= len - i;
  }
  for (int i = len - 1; i >= 0; i--) {
    for (int j = i - 1; j >= 0; j--) {
      if (ret[j] > ret[i]) continue;
      ret[i]++;
    }
  }
  for (int i = 0; i < len; i++) ret[i] = v[ret[i]];
  return ret;
}
void solve() {
  vector<long long> v;
  long long bnd = max(n - 15, 1LL);
  for (long long i = bnd; i <= n; i++) v.push_back(i);
  v = cal(v, --k);
  int ret = 0;
  for (int i = 0; i < num.size(); i++) {
    if (num[i] > bnd) break;
    ret++;
  }
  for (int i = 0; i < v.size(); i++) {
    long long p = bnd + i;
    long long m = v[i];
    if (valid(p) && valid(m)) ret++;
  }
  cout << ret << endl;
}
int main() {
  init();
  while (cin >> n >> k) {
    if (!init2()) continue;
    solve();
  }
}
