#include <bits/stdc++.h>
using namespace std;
int n, k;
int ans = 0;
long long fac[30];
map<int, bool> M;
bool checklucky(long long x) {
  for (; x; x /= 10)
    if (x % 10 != 4 && x % 10 != 7) return 0;
  return 1;
}
int check(int cur) {
  if (cur < max(n - 20 + 1, 1)) return checklucky(cur);
  int tmp = k;
  M.clear();
  for (int i = max(n - 20 + 1, 1); i <= cur; i++) {
    int x = max(n - 20 + 1, 1);
    while (M[x]) x++;
    while (tmp > fac[n - i]) {
      x++;
      tmp -= fac[n - i];
      while (M[x]) x++;
    }
    M[x] = 1;
    if (i == cur) return checklucky(x);
  }
}
void dfs(long long cur) {
  if (cur > n) return;
  ans += check(cur);
  dfs(cur * 10 + 4);
  dfs(cur * 10 + 7);
}
int main() {
  fac[0] = 1;
  for (int i = 1; i <= 23; i++) fac[i] = fac[i - 1] * i;
  cin >> n >> k;
  dfs(4);
  dfs(7);
  if (n <= 18 && k > fac[n]) {
    puts("-1");
    return 0;
  }
  cout << ans << endl;
  return 0;
}
