#include <bits/stdc++.h>
using namespace std;
long long fact[15];
vector<long long> luckies;
long long next(int x) {
  return *lower_bound(luckies.begin(), luckies.end(), x);
}
long long to_lucky(int x, int l) {
  long long res = 0;
  for (int i = 0; i < (l); ++i)
    if (x & (1 << i))
      res = res * 10 + 4;
    else
      res = res * 10 + 7;
  return res;
}
class A {
 public:
  A() {
    fact[0] = 1;
    for (int i = (1); i < (15); ++i) fact[i] = i * fact[i - 1];
  }
} __A;
class B {
 public:
  B() {
    for (int i = 0; i < (11); ++i)
      for (int j = 0; j < (1 << i); ++j) luckies.push_back(to_lucky(j, i));
    sort(luckies.begin(), luckies.end());
  }
} __B;
int res = 0;
int offset = 0;
vector<int> cutit(int n, int k) {
  if (n == 0) return vector<int>();
  long long base = k / fact[n - 1];
  long long mod = k % fact[n - 1];
  vector<int> t = cutit(n - 1, mod);
  t.push_back(base + 1);
  return t;
}
vector<int> to_permutation(vector<int>& vi) {
  vector<int> res;
  map<int, int> num;
  reverse(vi.begin(), vi.end());
  for (int i = 0; i < (vi.size()); ++i) num[i + offset + 1];
  for (int i = 0; i < (vi.size()); ++i) {
    int d = vi[i];
    int v = 0;
    for (typeof(num.begin()) it = num.begin(); it != num.end(); ++it) {
      d--;
      if (d == 0) {
        v = it->first;
        num.erase(it);
        break;
      }
    }
    res.push_back(v);
  }
  return res;
}
int isLucky(int x) {
  while (x) {
    if (x % 10 != 4 && x % 10 != 7) return false;
    x /= 10;
  }
  return true;
}
int main() {
  int n, k;
  cin >> n >> k;
  if (n > 14) {
    offset = n - 14;
    res = lower_bound(luckies.begin(), luckies.end(), offset) - luckies.begin();
    if (next(offset) > offset) res -= 1;
    n = 14;
  }
  if (k > fact[n]) {
    cout << -1;
    return 0;
  }
  vector<int> vi = cutit(n, k - 1);
  vi = to_permutation(vi);
  for (int i = 0; i < (vi.size()); ++i)
    if (isLucky(vi[i]) && isLucky(i + offset + 1)) res++;
  cout << res;
  return 0;
}
