#include <bits/stdc++.h>
using namespace std;
long long f[20];
vector<long long> lu;
void go(int x, int y, long long p) {
  if (x == y) return;
  lu.push_back(p * 10 + 4);
  lu.push_back(p * 10 + 7);
  go(x, y + 1, p * 10 + 4);
  go(x, y + 1, p * 10 + 7);
}
bool ok(long long x) {
  if (!x) return 0;
  while (x) {
    if (!(x % 10 == 4 || x % 10 == 7)) return 0;
    x /= 10;
  }
  return 1;
}
void radix(long long k, vector<long long> &num) {
  k--;
  int len = num.size();
  int aw, get;
  for (get = 1, aw = len - 1; get < len; get++, aw--)
    if (f[get] > k) break;
  get--;
  while (k) {
    long long a = k / f[get];
    k = k % f[get];
    swap(num[aw], num[aw + a]);
    sort(num.begin() + aw + 1, num.end());
    aw++;
    get--;
  }
}
int main() {
  go(13, 0, 0);
  sort(lu.begin(), lu.end());
  int p = lu.size();
  f[0] = 1;
  for (long long i = 1; i < 15; i++) f[i] = f[i - 1] * i;
  long long n, k;
  vector<long long> num;
  while (cin >> n >> k) {
    long long ans = 0;
    if (n > 13) {
      for (long long i = 12; i >= 0; i--) num.push_back(n - i);
      for (int i = 0; i < p; i++) {
        if (lu[i] <= n - 13) ans++;
      }
      radix(k, num);
      for (long long i = 12, j = 0; i >= 0; i--, j++) {
        if (ok(num[j]) && ok(n - i)) ans++;
      }
    } else if (f[n] >= k) {
      for (int i = 0; i < n; i++) num.push_back(i + 1);
      radix(k, num);
      for (int i = 0; i < n; i++) {
        if (ok(num[i]) && (ok(i + 1))) ans++;
      }
    }
    if (n <= 13)
      if (f[n] < k) ans = -1;
    cout << ans << endl;
  }
  return 0;
}
