#include <bits/stdc++.h>
using namespace std;
long long int fact[14];
int n, k;
int cs, s;
vector<int> v;
vector<long long int> lnum;
void Set() {
  fact[0] = 1;
  for (long long int i = 1; i < 14; i++) fact[i] = i * fact[i - 1];
}
void create_lucky(int i) {
  for (int i = cs; i < s; i++)
    lnum.push_back(lnum[i] * 10 + 4), lnum.push_back(lnum[i] * 10 + 7);
}
bool lucky(int num) {
  while (num) {
    if (num % 10 != 4 && num % 10 != 7) return false;
    num /= 10;
  }
  return true;
}
int main() {
  lnum.push_back(4);
  lnum.push_back(7);
  cs = 0;
  s = 2;
  for (int i = 2; i <= 9; i++) {
    create_lucky(i);
    cs = s;
    s = lnum.size();
  }
  lnum.push_back(4444444444);
  Set();
  cin >> n >> k;
  if (n <= 13 && fact[n] < k) {
    cout << -1 << endl;
    return 0;
  }
  int dig = 1;
  for (int i = 1; fact[i] < k; i++) dig++;
  for (int i = n - dig + 1; i <= n; i++) {
    v.push_back(i);
  }
  int cp = n - dig, cs = v.size(), pi = 1, cnt = 0;
  while (1) {
    int cn = 0;
    while (k > cn * fact[cs - 1]) cn++;
    k = k - (cn - 1) * fact[cs - 1];
    int cnum = v[cn - 1];
    int pos = cp + pi;
    pi++;
    v.erase(v.begin() + cn - 1);
    if (lucky(pos) && lucky(cnum)) cnt++;
    if (k == 1) {
      for (int i = 0; i < cs; i++)
        if (lucky(pos + i + 1) && lucky(v[i])) cnt++;
      break;
    }
    cs--;
  }
  for (int i = 0; lnum[i] <= cp; i++) cnt++;
  cout << cnt << endl;
  return 0;
}
