#include <bits/stdc++.h>
using namespace std;
int c(long long a, long long b) {
  if (a > b) return 0;
  return 1 + c(a * 10 + 4, b) + c(a * 10 + 7, b);
}
int is(long long a) {
  return a == 0 || (a % 10 == 4 || a % 10 == 7) && is(a / 10);
}
long long f[20];
bool u[20];
int main() {
  int n, k;
  cin >> n >> k;
  k--;
  f[0] = 1;
  for (int i = 1; i <= 13; i++) f[i] = f[i - 1] * i;
  if (n <= 13 && f[n] <= k) {
    cout << -1;
    return 0;
  }
  int ans = c(0, n) - 1;
  for (int i = 13; i >= 0; i--) {
    int shift = k / f[i], c = -1;
    while (shift >= 0) {
      shift -= !u[++c];
    }
    u[c] = 1;
    int t = c - 13 + n;
    if (t > 0) {
      if (is(n - i)) ans += is(t) - 1;
    }
    k %= f[i];
  }
  cout << ans;
}
