#include <bits/stdc++.h>
using namespace std;
const int N = 4e3 + 5;
const long long mod = 1e9 + 7;
long long power(long long a, long long b, long long c) {
  long long ret = 1;
  while (b > 0) {
    if (b % 2 == 1) ret = (ret * a) % c;
    b /= 2;
    a = (a * a) % c;
  }
  return ret;
}
int fact[13];
vector<int> values;
void pre() {
  fact[1] = fact[0] = 1;
  for (int i = 2; i <= 12; i++) {
    fact[i] = fact[i - 1] * i;
  }
}
void func(int ind, int val) {
  if (ind == 9) return;
  values.push_back(val * 10 + 4);
  values.push_back(val * 10 + 7);
  func(ind + 1, val * 10 + 4);
  func(ind + 1, val * 10 + 7);
}
int main() {
  pre();
  int n, k;
  cin >> n >> k;
  k--;
  func(0, 0);
  set<int> se;
  sort(values.begin(), values.end());
  for (int it : values) {
    se.insert(it);
  }
  vector<int> vec, res;
  int fl = -1;
  if (n <= 12 and fact[n] <= k) {
    cout << "-1\n";
    return 0;
  }
  for (int i = 12; i >= 0; i--) {
    if (k >= fact[i]) {
      if (fl == -1) {
        for (int j = 0; j <= i; j++) {
          vec.push_back(j + 1);
        }
        fl = i;
      }
      int times = k / fact[i];
      res.push_back(vec[times] + n - fl - 1);
      vec.erase(vec.begin() + times, vec.begin() + times + 1);
      k -= times * fact[i];
    } else if (fl >= 0) {
      res.push_back(vec[0] + n - fl - 1);
      vec.erase(vec.begin(), vec.begin() + 1);
    }
  }
  int ret = 0;
  for (int it : values) {
    if (it < n - fl) {
      ret++;
    }
  }
  int ind = n - fl;
  for (auto it : res) {
    if (se.find(it) != se.end() and se.find(ind) != se.end()) {
      ret++;
    }
    ind++;
  }
  cout << ret << endl;
  return 0;
}
