#include <bits/stdc++.h>
using namespace std;
const int MAX = 15;
long long fat[MAX];
vector<int> fn(int x) {
  vector<int> v;
  for (int i = 1; x; i++) {
    v.push_back(x % i);
    x /= i;
  }
  reverse(v.begin(), v.end());
  return v;
}
bool lucky(int x) {
  while (x) {
    if (x % 10 != 4 && x % 10 != 7) {
      return false;
    }
    x /= 10;
  }
  return true;
}
vector<string> guys;
string state = "";
void gen() {
  if (state.size() == 10) {
    return;
  }
  if (!state.empty()) {
    guys.push_back(state);
  }
  state += '4';
  gen();
  state.pop_back();
  state += '7';
  gen();
  state.pop_back();
}
int main() {
  fat[0] = 1;
  for (int i = int(1); i < int(MAX); i++) {
    fat[i] = i * fat[i - 1];
  }
  int n, k;
  scanf("%d %d", &n, &k);
  if (n <= 12 && k > fat[n]) {
    puts("-1");
    return 0;
  }
  vector<int> lasts;
  int to_roll = min(n, 13);
  int guy = n;
  while (to_roll--) {
    lasts.push_back(guy--);
  }
  reverse(lasts.begin(), lasts.end());
  int start = -1;
  int ptr = 1;
  for (int i = int(lasts.size() - 1); i >= int(0); i--) {
    if (fat[ptr++] >= k) {
      start = i;
      break;
    }
  }
  vector<int> v = fn(k - 1);
  vector<int> p;
  for (int i = int(0); i < int(v.size()); i++) {
    p.push_back(lasts[start + v[i]]);
    lasts.erase(lasts.begin() + start + v[i]);
  }
  int ans = 0;
  int index = n;
  for (int i = int(p.size() - 1); i >= int(0); i--) {
    if (lucky(index--) && lucky(p[i])) {
      ans++;
    }
  }
  int l = 1, r = index;
  gen();
  for (auto &each : guys) {
    int num = stoi(each);
    ans += l <= num && num <= r;
  }
  printf("%d\n", ans);
  return 0;
}
