#include <bits/stdc++.h>
using namespace std;
vector<int> v;
int main() {
  for (int i = 0; i < (1 << 9); i++) {
    int k = i, n = 0, j = 0;
    while (k > 0) {
      if (k % 2 == 1)
        n = (n * 10) + 7;
      else
        n = (n * 10) + 4;
      k /= 2;
      j++;
    }
    if (n != 0) v.push_back(n);
    for (; j < 9; j++) {
      n = (n * 10) + 4;
      v.push_back(n);
    }
  }
  sort(v.begin(), v.end());
  long long p = 1LL;
  int n, i, j;
  long long k;
  scanf("%d %lld", &n, &k);
  for (i = 1; i <= n; i++) {
    p = p * (long long)i;
    if (p >= k) break;
  }
  if (i == n + 1) {
    cout << "-1";
    return 0;
  }
  p = p / i;
  vector<int> second;
  j = i;
  for (i = n - i + 1; i <= n; i++) second.push_back(i);
  int ans = 0;
  int y = j;
  for (i = n - j + 1; i <= n; i++) {
    int m = k / p;
    if (k % p != 0) m++;
    m--;
    int first = 0, t;
    for (t = 0; t < v.size(); t++)
      if (v[t] == i) first++;
    for (t = 0; t < v.size(); t++)
      if (v[t] == second[m]) first++;
    if (first == 2) ans++;
    second.erase(second.begin() + m);
    if (k % p == 0)
      k = p;
    else
      k = k % p;
    j--;
    if (i != n) p = p / j;
  }
  ans = ans + upper_bound(v.begin(), v.end(), n - y) - v.begin();
  cout << ans;
  return 0;
}
