#include <bits/stdc++.h>
using namespace std;
void JIZZ() {
  cout << "";
  exit(0);
}
const long double PI = 3.14159265358979323846264338327950288;
const long double eps = 1e-13;
const long long mod = 1e9 + 7;
vector<long long> luc;
void init() {
  for (int dg = 1; dg <= 10; ++dg) {
    for (int i = 0; i < (1 << dg); ++i) {
      long long x = 0;
      for (int j = dg - 1; j >= 0; --j) {
        if (i & (1 << j)) {
          x = x * 10 + 7;
        } else
          x = x * 10 + 4;
      }
      luc.push_back(x);
    }
  }
}
long long fac[15], a[15], ori[15];
int main() {
  init();
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  fac[0] = 1;
  for (int i = 1; i <= 13; ++i) {
    fac[i] = fac[i - 1] * i;
  }
  int n, k;
  cin >> n >> k;
  if (n <= 13 && k > fac[n]) return cout << "-1" << endl, 0;
  if (k == 1) {
    auto it = upper_bound(luc.begin(), luc.end(), n);
    cout << it - luc.begin() << endl;
    exit(0);
  }
  int move = 1;
  while (fac[move] < k) ++move;
  ;
  ;
  int fix = n - move;
  int ans = upper_bound(luc.begin(), luc.end(), fix) - luc.begin();
  ;
  ;
  vector<long long> candi;
  for (int i = fix + 1, ptr = 0; i <= n; ++i, ++ptr)
    ori[ptr] = i, candi.push_back(i);
  ;
  ;
  --k;
  for (int i = move, ptr = 0; i > 0; --i, ++ptr) {
    int it = k / fac[i - 1];
    ;
    ;
    a[ptr] = candi[it];
    candi.erase(candi.begin() + it);
    ;
    ;
    k %= fac[i - 1];
  }
  for (int i = 0; i < move; ++i)
    if (*lower_bound(luc.begin(), luc.end(), a[i]) == a[i] &&
        *lower_bound(luc.begin(), luc.end(), fix + i + 1) == fix + i + 1)
      ++ans;
  cout << ans << endl;
}
