#include <bits/stdc++.h>
using namespace std;
long long fact[20], n, k;
bool isGood(long long x) {
  while (x) {
    if (x % 10 != 4 && x % 10 != 7) return 0;
    x /= 10;
  }
  return 1;
}
vector<long long> getkth(long long k, long long n, long long x) {
  vector<long long> v(n + 1, 0);
  for (long long i = 1; i <= n; ++i) {
    long long ok = 1, poz = 1;
    while (ok && poz <= n) {
      if (v[poz])
        poz++;
      else {
        if (k > fact[n - i])
          k -= fact[n - i];
        else
          break;
        poz++;
      }
    }
    v[poz] = x + i - 1;
  }
  return v;
}
long long getnext(long long x) {
  long long sol = x, p10 = 1, ok = 1;
  while (x && ok) {
    if (x % 10 == 4) {
      sol += p10 * 3;
      ok = 0;
    } else
      sol -= p10 * 3;
    p10 *= 10;
    x /= 10;
  }
  if (ok) sol += p10 * 4;
  return sol;
}
long long howmany(long long n) {
  long long x = 4, sol = 0;
  while (x <= n) sol++, x = getnext(x);
  return sol;
}
int32_t main() {
  fact[0] = 1;
  for (long long i = 1; i <= 14; ++i) fact[i] = fact[i - 1] * i;
  cin >> n >> k;
  if (n <= 14 && k > fact[n]) {
    cout << -1 << "\n";
    return 0;
  }
  long long sol = 0;
  if (n <= 14) {
    vector<long long> v = getkth(k, n, 1);
    for (long long i = 1; i <= n; ++i) {
      if (isGood(i) && isGood(v[i])) sol++;
    }
  } else {
    sol += howmany(n - 14);
    vector<long long> v = getkth(k, 14, n - 13);
    for (long long i = 1; i <= 14; ++i) {
      if (isGood(i + n - 14) && isGood(v[i])) sol++;
    }
  }
  cout << sol << "\n";
  return 0;
}
