#include <bits/stdc++.h>
using namespace std;
map<long long, int> lucky;
void func(long long num) {
  if (num > 1000000007) return;
  if (num > 0) {
    lucky[num] = 1;
  }
  func(num * 10 + 4);
  func(num * 10 + 7);
}
int main() {
  func(0);
  vector<long long> fact(21);
  fact[0] = 1;
  for (int i = 1; i < 21; i++) {
    fact[i] = fact[i - 1] * i;
  }
  int n;
  long long k;
  cin >> n >> k;
  if (n <= 20 && fact[n] < k) {
    cout << -1;
    return 0;
  }
  int ans = 0;
  for (auto g : lucky) {
    if (g.first < n - 20) ans++;
  }
  map<int, bool> vis;
  for (int i = max(n - 20, 1); i <= n; i++) {
    if (fact[n - i] < k) {
      long long val = 0;
      for (int j = max(n - 20, 1); j <= n; j++) {
        if (!vis[j]) {
          val += fact[n - i];
          if (val >= k) {
            ans += (lucky[i] == true && lucky[j] == true);
            vis[j] = true;
            k -= (val - fact[n - i]);
            break;
          }
        }
      }
    } else {
      for (int j = max(n - 20, 1); j <= n; j++) {
        if (!vis[j]) {
          ans += (lucky[i] == true && lucky[j] == true);
          vis[j] = true;
          break;
        }
      }
    }
  }
  cout << ans;
}
