#include <bits/stdc++.h>
using namespace std;
vector<long long> v;
void CalcLucky(long long num, long long limit) {
  long long temp4;
  temp4 = num * 10 + 4;
  long long temp7;
  temp7 = num * 10 + 7;
  v.push_back(temp4);
  v.push_back(temp7);
  if (temp4 <= limit) CalcLucky(temp4, limit);
  if (temp7 <= limit) CalcLucky(temp7, limit);
}
int main() {
  long long fact[20] = {0};
  fact[0] = 1;
  for (int i = 1; i <= 13; i++) {
    fact[i] = i * fact[i - 1];
  }
  long long n, k;
  cin >> n >> k;
  if (n <= 13) {
    if (k > fact[n]) {
      cout << -1 << endl;
      return 0;
    }
  }
  k = k - 1;
  long long limit;
  for (int i = 0; i <= 13; i++) {
    if (fact[i] > k) {
      limit = i;
      break;
    }
  }
  int numbers[20];
  int indices[20];
  int divisor = 1;
  for (int place = 1; place <= n; place++) {
    if ((k / divisor) == 0) break;
    indices[limit - place] = (k / divisor) % place;
    divisor *= place;
  }
  long long stIn = n - limit + 1;
  for (int i = 0; i < limit; i++) numbers[i] = stIn + i;
  for (int i = 0; i < limit; i++) {
    long long index = indices[i] + i;
    if (index != i) {
      long long temp = numbers[index];
      for (int j = index; j > i; j--) numbers[j] = numbers[j - 1];
      numbers[i] = temp;
    }
  }
  CalcLucky(0, stIn);
  int LuckyNum = 0;
  sort(v.begin(), v.end());
  for (int i = 0; i < v.size(); i++)
    if (v[i] < stIn) LuckyNum++;
  for (int i = 0; i < limit; i++) {
    long long index = stIn + i;
    long long num = numbers[i];
    if (binary_search(v.begin(), v.end(), index) &&
        binary_search(v.begin(), v.end(), num))
      LuckyNum++;
  }
  cout << LuckyNum << endl;
}
