#include <bits/stdc++.h>
using namespace std;
long long F[20];
vector<int> lucky;
long long n;
void get(long long num = 0, int tam = 0) {
  if (num > n) return;
  if (tam) lucky.push_back(num);
  get(num * 10 + 4, tam + 1);
  get(num * 10 + 7, tam + 1);
}
void permuta(vector<int>& x, long long k) {
  int tam = x.size();
  for (int i = 0, quedan = tam - 1; i < tam; i++, quedan--) {
    sort(x.begin() + i, x.end());
    int mueren = 0;
    while (k > F[quedan]) mueren++, k -= F[quedan];
    swap(x[i], x[i + mueren]);
  }
}
int main() {
  long long k;
  cin >> n >> k;
  vector<int> lasts;
  for (int i = n; lasts.size() < 15 && i >= 1; i--) lasts.push_back(i);
  F[0] = 1;
  for (int i = 1; i < 17; i++) F[i] = F[i - 1] * i;
  if (F[lasts.size()] < k) {
    cout << -1 << endl;
    return 0;
  }
  get();
  sort(lucky.begin(), lucky.end());
  permuta(lasts, k);
  int antes = n - lasts.size();
  int res = 0;
  for (int i = 0; i < lucky.size(); i++)
    if (lucky[i] <= antes)
      res++;
    else {
      int pos = find(lasts.begin(), lasts.end(), lucky[i]) - lasts.begin();
      pos += antes + 1;
      res += binary_search(lucky.begin(), lucky.end(), pos);
    }
  cout << res << endl;
}
