#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100099;
int n, k;
vector<long long> lucky;
void gen(long long num) {
  lucky.push_back(num);
  if (num > n) return;
  gen(num * 10 + 4);
  gen(num * 10 + 7);
}
vector<int> d;
long long precalc[20];
bool used[20];
void findk(int n, int k, int len) {
  if (len < 0) return;
  long long sum = 0;
  for (int dig = 1; dig <= n; dig++) {
    if (!used[dig]) {
      if (sum + precalc[len] >= k) {
        used[dig] = 1;
        d.push_back(dig);
        break;
      } else
        sum += precalc[len];
    }
  }
  findk(n, k - sum, len - 1);
}
bool check(int n, int k, int pos) {
  for (int i = 0, _n = (20); i < _n; i++) used[i] = 0;
  d.clear();
  findk(n, k, n - 1);
  if (d[pos - 1] == pos) return 1;
  return 0;
}
bool good(long long t) {
  while (t) {
    if (t % 10 != 7 && t % 10 != 4) return 0;
    t /= 10;
  }
  return 1;
}
int main() {
  scanf("%d%d", &n, &k);
  gen(4);
  gen(7);
  long long fact = 1;
  precalc[0] = 1;
  precalc[1] = 1;
  for (int i = 2; i < 20; i++) precalc[i] = precalc[i - 1] * i;
  int p = 0;
  for (int i = 1; i <= n + 1; i++) {
    fact *= i;
    p++;
    if (fact >= k) break;
  }
  if (p > n) {
    printf("-1\n");
    return 0;
  }
  int last = n - p;
  int ans = 0;
  sort(lucky.begin(), lucky.end());
  for (int i = 0; i < lucky.size(); i++) {
    if (lucky[i] <= last) ans++;
    if (lucky[i] > n) break;
    if (lucky[i] > last) {
      for (int i = 0, _n = (20); i < _n; i++) used[i] = 0;
      d.clear();
      findk(p, k, p - 1);
      for (int i = (n - p + 1), _b = (n); i <= _b; i++)
        if (good(i) && good(d[i - last - 1] + last)) ans++;
      break;
    }
  }
  printf("%d\n", ans);
  return 0;
}
