#include <bits/stdc++.h>
using namespace std;
vector<int> lucky;
void init() {
  lucky.push_back(0);
  for (size_t i = 0; i < lucky.size(); i++) {
    if (lucky[i] >= 100000000) break;
    lucky.push_back(lucky[i] * 10 + 4);
    lucky.push_back(lucky[i] * 10 + 7);
  }
  lucky.erase(lucky.begin());
}
bool islucky(int x) {
  do
    if (x % 10 != 7 && x % 10 != 4) return false;
  while (x /= 10);
  return true;
}
int main() {
  init();
  int n, m, len = 0, ans = 0;
  long long s = 1;
  scanf("%d%d", &n, &m);
  while (s < m) s *= ++len;
  if (n < len) return puts("-1") & 0;
  vector<int> u;
  for (int i = 1; i <= len; i++) u.push_back(n - len + i);
  for (int i = 1; i <= len; i++) {
    s /= len + 1 - i;
    int x = (m - 1) / s;
    if (islucky(n - len + i) && islucky(u[x])) ans++;
    u.erase(u.begin() + x);
    m -= x * s;
  }
  ans += upper_bound(lucky.begin(), lucky.end(), n - len) - lucky.begin();
  printf("%d\n", ans);
}
