#include <bits/stdc++.h>
using namespace std;
const long long dx[] = {4LL, 7LL};
vector<int> lucky;
vector<long long> P;
int main() {
  P.push_back(1);
  for (long long i = 1; P.back() <= 1000000000; i++) P.push_back(P[i - 1] * i);
  int n, k;
  cin >> n >> k;
  if (n < 13 && P[n] < k) {
    cout << -1;
    return (0);
  }
  int tmp;
  list<int> L;
  for (int i = 1, x = n; i < P.size(); i++, x--) {
    L.push_front(x);
    if (P[i] >= k) {
      tmp = x;
      break;
    }
  }
  vector<int> perm;
  for (int i = L.size() - 1; i >= 0; i--) {
    list<int>::iterator it = L.begin();
    while (k > P[i]) {
      k -= P[i];
      it++;
    }
    perm.push_back(*it);
    L.erase(it);
  }
  {
    queue<int> Q;
    Q.push(0);
    while (!Q.empty()) {
      int x = Q.front();
      for (int i = 0; i < 2; i++) {
        long long x1 = x * 10LL + dx[i];
        if (x1 <= 1000000000) {
          lucky.push_back(x1);
          Q.push(x1);
        }
      }
      Q.pop();
    }
  }
  int res = 0;
  for (int i = 0; i < lucky.size() && lucky[i] < tmp; i++) res++;
  for (int i = 0, p = tmp; i < perm.size(); i++, p++) {
    bool f1 = false;
    bool f2 = false;
    for (int j = 0; j < lucky.size(); j++) {
      if (perm[i] == lucky[j]) f1 = true;
      if (p == lucky[j]) f2 = true;
    }
    if (f1 && f2) res++;
  }
  cout << res;
exit:
  return (0);
}
