#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 10;
const int MAXN = 1e4 + 10;
const int MOD = 1e9 + 7;
const int inf = 1e9;
const double pi = acos(-1.0);
const double eps = 1e-6;
int dx[] = {0, -1, 0, 1};
int dy[] = {1, 0, -1, 0};
long long F[40];
int calc(int l, long long n) {
  if (n >= l) return 0;
  int res = 0;
  res += calc(l, n * 10 + 4);
  res += calc(l, n * 10 + 7);
  return res + 1;
}
bool check(int x) {
  bool ok = true;
  while (x) {
    int t = x % 10;
    if (t != 4 && t != 7) ok = false;
    x /= 10;
  }
  return ok;
}
int n, k;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> k;
  F[0] = 1LL;
  int pos = -1;
  for (int i = 1; i <= n; i++) {
    F[i] = F[i - 1] * (long long)i;
    if (F[i] >= (long long)k) {
      pos = i;
      break;
    }
  }
  if (pos == -1) {
    cout << -1;
    return 0;
  }
  int s = n - pos + 1;
  int res = calc(s, 4) + calc(s, 7);
  vector<int> V;
  for (int i = s; i <= n; i++) V.push_back(i);
  for (int i = s; i <= n; i++) {
    int j = n - i;
    int z, t;
    if (F[j] <= k) {
      z = (k - 1) / F[j];
      t = V[z];
      k = (k - 1) % F[j] + 1;
    } else {
      z = 0;
      t = V[0];
    }
    V.erase(V.begin() + z);
    if (check(t) && check(i)) res++;
  }
  cout << res;
  return 0;
}
