#include <bits/stdc++.h>
using namespace std;
long long fact[22] = {1};
vector<int> solve(int n, long long k) {
  int i, j, r;
  vector<int> ans;
  set<int> st;
  for (i = n - 1; i >= 0; i--) {
    int t = (k - 1) / fact[i] + 1;
    k -= (t - 1) * fact[i];
    r = 0;
    for (j = 1; j <= n; j++)
      if (st.find(j) == st.end()) {
        r++;
        if (r == t) break;
      }
    ans.push_back(j);
    st.insert(j);
  }
  return ans;
}
long long luck[2024];
int size = 0;
void back(int i, long long num) {
  if (i > 8) return;
  luck[size++] = num;
  back(i + 1, 10 * num + 4);
  back(i + 1, 10 * num + 7);
}
int go(int n) {
  int i;
  for (i = 0; luck[i] <= n; i++)
    ;
  return i;
}
bool islucky(int n) {
  while (n != 0) {
    if (!(n % 10 == 4 || n % 10 == 7)) return false;
    n /= 10;
  }
  return true;
}
int main() {
  int n, i, j, r, ans = 0;
  long long k;
  back(0, 4);
  back(0, 7);
  luck[size++] = 4444444444L;
  sort(luck, luck + size);
  for (i = 1; i <= 18; i++) fact[i] = i * fact[i - 1];
  cin >> n >> k;
  if (n < 15) {
    if (k > fact[n]) {
      cout << -1 << endl;
      return 0;
    }
  }
  vector<int> ans1;
  if (n > 13) {
    ans1 = solve(13, k);
    for (i = 0; i < 13; i++) {
      if (islucky(i + n - 13 + 1) && islucky(ans1[i] + n - 13)) ans++;
    }
    ans += go(n - 13);
  } else {
    ans1 = solve(n, k);
    for (i = 0; i < n; i++) {
      if (islucky(i + 1) && islucky(ans1[i])) ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
