#include <bits/stdc++.h>
using namespace std;
const long long MAX = 1000000000ll;
const long double EPS = 1E-9;
const int INF = (int)1E9;
const long long INF64 = (long long)1E18;
const long double PI = 2 * acos(.0);
long long fact[15];
void factorial(int x) {
  fact[0] = 1;
  fact[1] = 1;
  for (int i = 2; i < (int)(x + 1); i++) fact[i] = i * fact[i - 1];
  return;
}
vector<long long> kthPerm(int x, int k) {
  bool used[15];
  for (int i = 0; i < (int)(x + 1); i++) used[i] = 0;
  long long base = 0;
  vector<long long> resp;
  for (int i = 0; i < (int)(x); i++) {
    long long rad = fact[x - i - 1];
    for (int j = 1; j < (int)(x + 1); j++) {
      if (used[j]) continue;
      if (base + rad >= k) {
        resp.push_back(j);
        used[j] = 1;
        break;
      } else {
        base += rad;
      }
    }
  }
  return resp;
}
vector<long long> luck;
void genLucky(long long i) {
  if (i >= 1000000000ll) return;
  long long k = i * 10ll + 7ll;
  long long l = i * 10ll + 4ll;
  luck.push_back(k);
  luck.push_back(l);
  genLucky(k);
  genLucky(l);
}
bool isL(int x) {
  while (x > 0) {
    int y = x % 10;
    if (!(y == 4 || y == 7)) return 0;
    x /= 10;
  }
  return 1;
}
int main() {
  int n, k;
  cin >> n >> k;
  factorial(13);
  int cont = 0;
  if (n < 13) {
    if (k > fact[n])
      cout << "-1" << endl;
    else {
      vector<long long> pp = kthPerm(n, k);
      for (int i = 0; i < (int)((pp).size()); i++) {
        if (isL(pp[i]) && isL(i + 1)) cont++;
      }
      cout << cont << endl;
    }
  } else {
    vector<long long> pp = kthPerm(13, k);
    genLucky(0ll);
    int left = n - 13;
    for (int i = 0; i < (int)((luck).size()); i++) {
      if (luck[i] <= (left)) {
        cont++;
      }
    }
    for (int i = 0; i < (int)((pp).size()); i++) {
      pp[i] += left;
    }
    for (int i = 0; i < (int)(13); i++) {
      if (isL(pp[i]) && isL(left + i + 1)) cont++;
    }
    cout << cont << endl;
  }
  return 0;
}
