#include <bits/stdc++.h>
std::vector<long long> generateLuckyNumbers(long long limit) {
  std::vector<long long> result;
  std::queue<long long> Q;
  Q.push(4);
  Q.push(7);
  while (!Q.empty()) {
    long long x = Q.front();
    Q.pop();
    if (x <= limit) {
      result.push_back(x);
      Q.push(x * 10 + 4);
      Q.push(x * 10 + 7);
    }
  }
  sort(result.begin(), result.end());
  return result;
}
inline bool is_lucky(long long n) {
  std::stringstream ss;
  ss << n;
  std::string s;
  ss >> s;
  for (int i = 0; i < (s.size()); ++i)
    if (s[i] != '4' && s[i] != '7') {
      return false;
    }
  return true;
}
long long silnie[16];
bool used[16];
int main() {
  silnie[0] = 1;
  for (int i = (1); i <= (14); ++i) silnie[i] = silnie[i - 1] * i;
  long long n, k;
  std::cin >> n >> k;
  long long limit = 1, silnia = 1;
  while (silnia < k) {
    ++limit;
    silnia *= limit;
  }
  if (limit > n) {
    std::cout << -1 << std::endl;
    return 0;
  }
  std::vector<int> permutacja;
  for (int i = 0; i < (limit); ++i) {
    used[i] = false;
  }
  for (int i = 0; i < (limit); ++i) {
    int wskaznik = 0;
    while (used[wskaznik]) {
      ++wskaznik;
    }
    while (k > silnie[limit - i - 1]) {
      k -= silnie[limit - i - 1];
      do {
        ++wskaznik;
      } while (used[wskaznik]);
    }
    permutacja.push_back(wskaznik);
    used[wskaznik] = true;
  }
  std::vector<long long> luckyNums = generateLuckyNumbers(n);
  std::vector<int> wartosci;
  for (int i = (n - limit + 1); i <= (n); ++i) {
    if (std::find(luckyNums.begin(), luckyNums.end(), i) != luckyNums.end()) {
      wartosci.push_back(1);
    } else {
      wartosci.push_back(0);
    }
  }
  int dodatek = 0;
  for (int i = 0; i < (limit); ++i)
    if (wartosci[i] && is_lucky(permutacja[i] + n - limit + 1)) {
      ++dodatek;
    }
  int ilu_najpierw =
      std::upper_bound(luckyNums.begin(), luckyNums.end(), n - limit) -
      luckyNums.begin();
  std::cout << ilu_najpierw + dodatek << std::endl;
}
