#include <bits/stdc++.h>
using namespace std;
int n, k;
int mx[15];
int cal(int ix) {
  if (ix == 0) return 0;
  long long temp = 1, i;
  for (i = 1;; i++) {
    temp *= i;
    if (temp >= ix) break;
  }
  int p = i - 1;
  if (temp == ix) {
    reverse(mx, mx + p + 1);
    return 0;
  }
  int t = temp / i;
  t = (ix - 1) / t;
  swap(mx[p], mx[p - t]);
  sort(mx, mx + p);
  reverse(mx, mx + p);
  cal(ix - temp / i * t);
  return p;
}
bool islucky(int x) {
  while (x) {
    int t = x % 10;
    if (t != 4 && t != 7) return 0;
    x /= 10;
  }
  return 1;
}
int res;
long long np;
void per(long long x) {
  if (x >= np) return;
  if (x) res++;
  per(x * 10 + 4);
  per(x * 10 + 7);
}
int main() {
  cin >> n >> k;
  long long temp = 1;
  bool sign = false;
  for (int i = 1; i <= n; i++) {
    temp *= i;
    if (temp >= k) {
      sign = true;
      break;
    }
  }
  if (!sign) {
    cout << "-1" << endl;
    return 0;
  }
  int nn = n;
  for (int i = 0; i <= 14; i++) {
    mx[i] = nn--;
  }
  int p = cal(k);
  np = n - p;
  per(0);
  nn = n;
  for (int i = 0; i <= p; i++) {
    if (islucky(mx[i]) && islucky(nn)) res++;
    nn--;
  }
  cout << res << endl;
  return 0;
}
