#include <bits/stdc++.h>
bool DEBUG = false;
using namespace std;
int val[5000];
int l, cur, n, k, result;
int sel[15], sl, perm[15];
bool used[15];
int fact[13], os;
int main() {
  l = 0;
  val[l++] = 4;
  val[l++] = 7;
  cur = 0;
  while (l < 1000) {
    int tmp = l;
    for (int i = (cur); i < (tmp); ++i) {
      val[l++] = val[i] * 10 + 4;
      val[l++] = val[i] * 10 + 7;
    }
    cur = tmp;
  }
  scanf("%d%d", &n, &k);
  result = 0;
  for (int i = 0; i < (l); ++i)
    if (val[i] < n - 12) result++;
  fact[0] = 1;
  for (int i = (1); i < (13); ++i) fact[i] = i * fact[i - 1];
  if (n > 13)
    os = n - 12;
  else
    os = 1;
  sl = 0;
  for (int i = (os); i <= (n); ++i) sel[sl++] = i;
  if (DEBUG) {
    cout << "sel"
         << ": ";
    for (int innercounter = 0; innercounter < (sl); ++innercounter)
      cout << sel[innercounter] << " ";
    cout << endl;
  };
  if (sl < 13 && k > fact[sl]) {
    puts("-1");
    return 0;
  }
  memset(used, 0, sizeof(used));
  for (int i = 0; i < (sl); ++i) {
    int shft = (k - 1) / fact[sl - i - 1];
    if (DEBUG) {
      cout << "shft"
           << ": " << (shft) << endl;
    };
    cur = 0;
    for (int j = 0; j < (sl); ++j)
      if (!used[j]) {
        if (DEBUG) {
          cout << "sel[j]"
               << ": " << (sel[j]) << endl;
        };
        if (cur < shft)
          cur++;
        else {
          perm[i] = sel[j];
          used[j] = true;
          break;
        }
      }
    k -= shft * fact[sl - i - 1];
  }
  if (DEBUG) {
    cout << "perm"
         << ": ";
    for (int innercounter = 0; innercounter < (sl); ++innercounter)
      cout << perm[innercounter] << " ";
    cout << endl;
  };
  set<int> vals;
  for (int i = 0; i < (l); ++i) vals.insert(val[i]);
  if (DEBUG) {
    cout << "os"
         << ": " << (os) << endl;
  };
  if (DEBUG) {
    cout << "n"
         << ": " << (n) << endl;
  };
  if (DEBUG) {
    cout << "l"
         << ": " << (l) << endl;
  };
  for (int i = 0; i < (l); ++i) {
    if (val[i] >= os && val[i] <= n) {
      if (DEBUG) {
        cout << "val[i]"
             << ": " << (val[i]) << endl;
      };
      if (vals.find(perm[val[i] - os]) != vals.end()) result++;
    }
  }
  cout << result;
  return 0;
}
