#include <bits/stdc++.h>
using namespace std;
set<long long> S;
void go(const long long &x, const int &M, const long long &U) {
  if (x > U) return;
  if (x > M) S.insert(x);
  go(10 * x + 4, M, U), go(10 * x + 7, M, U);
}
bool ok(long long x) {
  for (; x; x /= 10) {
    int a = x % 10;
    if (a != 4 && a != 7) return false;
  }
  return true;
}
int main() {
  long long N, K;
  cin >> N >> K;
  int M = 1;
  for (long long f = 1; f * M < K; f *= M, M++)
    ;
  if (M > (int)N) {
    cout << -1 << endl;
    return 0;
  }
  M = min((int)N, 20);
  go(0, 0, N - M);
  vector<long long> a(M);
  for (int _n(M), i(0); i < _n; i++) a[i] = N - M + i + 1;
  for (long long k = K - 1; k > 0;) {
    long long f = 1;
    int i = 1;
    while (f * (i + 1) <= k) f *= ++i;
    int j = k / f;
    swap(a[M - i - 1], a[M - i + j - 1]);
    sort(&a[0] + M - i, &a[0] + M);
    k -= f * j;
  }
  int ans = 0;
  for (int _n(M), i(0); i < _n; i++)
    if (ok(N - M + i + 1) && ok(a[i])) ans++;
  ans += S.size();
  cout << ans << endl;
  return 0;
}
