#include <bits/stdc++.h>
using namespace std;
long long n, k, num = 1;
long long kol = 1, RES = 0;
vector<int> mas, other;
vector<int> all;
int main() {
  cin >> n >> k;
  long long num = 4;
  while (num <= n) {
    all.push_back(num);
    string s = "";
    int nn = num;
    while (nn) {
      if (nn % 10 == 4)
        s = s + '4';
      else
        s = s + '7';
      nn /= 10;
    }
    reverse(s.begin(), s.end());
    int ch = -1;
    for (int i = 0; i < s.length(); ++i)
      if (s[i] == '4') ch = i;
    if (ch == -1) {
      for (int i = 0; i < s.length(); ++i) s[i] = '4';
      s = s + '4';
    } else {
      s[ch] = '7';
      for (int i = ch + 1; i < s.length(); ++i) s[i] = '4';
    }
    num = 0;
    for (int i = 0; i < s.length(); ++i) num = num * 10 + (int)(s[i] - '0');
  }
  if (k == 1) {
    cout << all.size();
    return 0;
  }
  num = 1;
  while (num * kol < k) {
    num *= kol;
    ++kol;
  }
  if (kol > n) {
    cout << -1;
    return 0;
  }
  for (int i = 0; i < all.size(); ++i) {
    if (all[i] > n - kol) break;
    ++RES;
  }
  for (int i = n - kol; i < n; ++i) other.push_back(i + 1);
  while (true) {
    int p = k / num;
    if (k % num) ++p;
    if (p == 0) {
      for (int i = other.size() - 1; i >= 0; --i) mas.push_back(other[i]);
      break;
    }
    mas.push_back(other[p - 1]);
    other.erase(other.begin() + p - 1);
    --kol;
    k = k % num;
    num /= kol;
    if (kol == 0) break;
  }
  for (int i = 0; i < all.size(); ++i) {
    int ch;
    if (all[i] - (n - mas.size()) - 1 >= 0)
      ch = mas[all[i] - (n - mas.size()) - 1];
    for (int j = 0; j < all.size(); ++j)
      if (ch == all[j]) ++RES;
  }
  cout << RES;
  return 0;
}
