#include <bits/stdc++.h>
using namespace std;
long long fac[15];
vector<int> getKthPerm(vector<int> arr, long long k) {
  if (k == 1) return arr;
  int n = arr.size();
  for (int i = 1; i <= n; i++) {
    if (k <= fac[n - 1] * i) {
      vector<int> brr;
      for (int j = 0; j < n; j++) {
        if (j + 1 == i) continue;
        brr.push_back(arr[j]);
      }
      vector<int> tmp = getKthPerm(brr, k - fac[n - 1] * i + fac[n - 1]);
      vector<int> ret;
      ret.push_back(arr[i - 1]);
      for (auto x : tmp) {
        ret.push_back(x);
      }
      return ret;
    }
  }
}
vector<int> getLuckyNumbers() {
  vector<int> ret({4, 7});
  for (int i = 1; i < 9; i++) {
    vector<int> tmp({4, 7});
    for (auto x : ret) {
      tmp.push_back(10 * x + 4);
      tmp.push_back(10 * x + 7);
    }
    ret = tmp;
  }
  sort(ret.begin(), ret.end());
  return ret;
}
bool isLucky(int n) {
  while (n) {
    int d = n % 10;
    if (d != 4 && d != 7) return false;
    n /= 10;
  }
  return true;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << setprecision(32);
  long long n, k;
  cin >> n >> k;
  fac[0] = 1;
  for (int i = 1; i < 15; i++) {
    fac[i] = fac[i - 1] * i;
  }
  if (n < 15) {
    if (fac[n] < k) {
      cout << -1 << endl;
      return 0;
    }
    vector<int> arr;
    for (int i = 1; i <= n; i++) {
      arr.push_back(i);
    }
    arr = getKthPerm(arr, k);
    int ans = 0;
    if (n >= 4 && isLucky(arr[3])) ans++;
    if (n >= 7 && isLucky(arr[6])) ans++;
    cout << ans << endl;
  } else {
    vector<int> arr;
    for (int i = 1; i < 15; i++) {
      arr.push_back(n - 14 + i);
    }
    arr = getKthPerm(arr, k);
    vector<int> lk = getLuckyNumbers();
    int ans = 0;
    for (auto x : lk) {
      if (x > n) break;
      if (x < n - 13)
        ans++;
      else if (isLucky(arr[x - n + 13]))
        ans++;
    }
    cout << ans << endl;
  }
  return 0;
}
