#include <bits/stdc++.h>
using namespace std;
long long N, K;
long long f[15];
int count(long long x, long long k) {
  if (x > k) return 0;
  return 1 + count(10 * x + 4, k) + count(10 * x + 7, k);
}
bool check(long long x) {
  if (x == 0) return false;
  while (x) {
    long long r = x % 10;
    if (r != 4 && r != 7) return false;
    x /= 10;
  }
  return true;
}
int main() {
  cin >> N >> K;
  f[1] = 1;
  for (int i = 2; i <= 14; i++) f[i] = f[i - 1] * i;
  if (N <= 14 && K > f[N]) {
    cout << -1 << endl;
    return 0;
  }
  int ans = count(0, max(N - 13, 0ll)) - 1;
  vector<int> t;
  for (int i = N - 12; i <= N; i++) t.push_back(i);
  K--;
  for (int i = 13; i > 1 && t.size() > 0; i--) {
    int tmp = K / f[i - 1];
    if (check(N - i + 1) && check(t[tmp])) ans++;
    t.erase(t.begin() + tmp);
    K %= f[i - 1];
  }
  if (check(N) && check(t[0])) ans++;
  cout << ans << endl;
  return 0;
}
