#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 11;
const long long MOD = 1e18;
long long n, k;
int ans = 0;
vector<long long> vv;
bool good(long long c) {
  while (c > 0) {
    if (c % 10 == 4 || c % 10 == 7) {
    } else
      return false;
    c /= 10;
  }
  return true;
}
void dfs(long long l) {
  if (l > n) return;
  if (l >= 1) {
    if (l < n - 15ll) {
      ans++;
    } else {
      long long pos = l - max(1ll, (n - 15));
      if (good(vv[pos])) ans++;
    }
  }
  dfs(l * 10ll + 4ll);
  dfs(l * 10ll + 7ll);
}
long long kol_perm(long long x) {
  long long k = 1;
  for (long long i = 1; i <= x; i++) k *= i;
  return k;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  if (n <= 15 && kol_perm(n) < k) {
    cout << "-1" << endl;
    return 0;
  }
  vector<long long> v, dd;
  for (int i = max(1ll, n - 15ll); i <= n; i++) v.push_back(i);
  int t = min(16ll, n);
  for (int i = 1; i <= t; i++) {
    int c = 0;
    for (long long j = 0; j < v.size(); j++)
      if (kol_perm(t - i) * (j + 1) >= k) {
        vv.push_back(v[j]);
        c = j;
        k -= kol_perm(t - i) * j;
        break;
      }
    dd.clear();
    for (int j = 0; j < c; j++) dd.push_back(v[j]);
    for (int j = c + 1; j < v.size(); j++) dd.push_back(v[j]);
    v = dd;
  }
  dfs(0);
  cout << ans << endl;
}
