#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
long long f[15], all[15];
long long n, k;
int solve(int rem) {
  if (!rem) return 1;
  return solve(rem - 1) * 2;
}
long long solve(int i, string& str) {
  if (i == str.size()) return 1;
  long long ret = solve(i + 1, str);
  if (str[i] == '1') ret += all[str.size() - i - 1];
  return ret;
}
bool lucky(long long x) {
  while (x) {
    if (x % 10 != 4 && x % 10 != 7) return 0;
    x /= 10;
  }
  return 1;
}
int main() {
  all[0] = f[0] = 1;
  for (int i = 1; i < 15; ++i) {
    f[i] = f[i - 1] * i;
    all[i] = 2ll * all[i - 1];
  }
  cin >> n >> k;
  if (n < 15 && f[n] < k) return cout << -1, 0;
  long long nn = n;
  int rem = 0;
  for (int i = 0; i < 14; ++i)
    if (f[i] < k) rem = i;
  long long ans = 0;
  set<long long> rem_num;
  if (n > rem + 1) {
    long long m = n - rem - 1;
    string cur = "", s = "";
    stringstream mycin;
    mycin << m;
    mycin >> cur;
    bool ok = 0, is = 0;
    int lstseven = -1;
    for (int i = 0; i < cur.size(); ++i) {
      int ch = cur[i] - '0';
      if (ok) break;
      if (ch < 4) {
        is = 1;
        break;
      } else if (ch > 7)
        ok = 1, lstseven = 1e9 + 5;
      else if (ch == 7)
        lstseven = max(lstseven, i);
      else if (ch > 4)
        ok = 1;
    }
    if (is && lstseven == -1) {
      int sz = cur.size();
      --sz;
      while (sz--) s += '1';
    } else {
      ok = 0;
      for (int i = 0; i < cur.size(); ++i) {
        int ch = cur[i] - '0';
        if (ok)
          s += '1';
        else if (is && lstseven == i)
          s += '0', ok = 1;
        else if (ch > 7)
          ok = 1, s += '1';
        else if (ch == 7)
          s += '1';
        else if (ch > 4)
          s += '0', ok = 1;
        else
          s += '0';
      }
    }
    if (s.size()) {
      ans += solve(0, s);
      for (int i = 1; i < s.size(); ++i) ans += all[i];
    }
    for (int i = n; rem_num.size() <= rem; --i) rem_num.insert(i);
    n = rem + 1;
  } else
    for (int i = 1; i <= n; ++i) rem_num.insert(i);
  for (int i = nn - n + 1; i <= nn; ++i) {
    vector<int> v;
    while (f[nn - i] < k) {
      k -= f[nn - i];
      v.push_back(*rem_num.begin());
      rem_num.erase(rem_num.begin());
    }
    int x = *rem_num.begin();
    rem_num.erase(rem_num.begin());
    for (int j : v) rem_num.insert(j);
    ans += lucky(i) && lucky(x);
  }
  cout << ans;
  return 0;
}
