#include <bits/stdc++.h>
using namespace std;
template <class T>
string i2s(T x) {
  ostringstream o;
  o << x;
  return o.str();
}
int s2i(string x) {
  int r = 0;
  istringstream sin(x);
  sin >> r;
  return r;
}
long long fact[14];
int N, K;
bool isLucky(int x) {
  string s = i2s(x);
  for (int i = 0; i < s.size(); i++)
    if (s[i] != '4' && s[i] != '7') return false;
  return true;
}
int countLucky(long long val, int mx) {
  if (val > mx) return 0;
  int ans = 0;
  if (val != 0) ans += 1;
  ans += countLucky(val * 10 + 4, mx);
  ans += countLucky(val * 10 + 7, mx);
  return ans;
}
int solve() {
  int i, j, sol = 0;
  fact[0] = fact[1] = 1;
  for (i = 2; i < 14; i++) fact[i] = i * fact[i - 1];
  int pos = 0;
  for (i = 0; i < 14; i++)
    if (fact[i] >= K) {
      pos = i;
      break;
    }
  if (pos > N) return -1;
  if (pos < N) sol += countLucky(0, N - pos);
  int perm[14];
  bool flag[14];
  memset(flag, false, sizeof(flag));
  for (i = 1; i <= pos; i++) {
    long long cnt = 0;
    for (j = 1; j <= pos; j++)
      if (!flag[j]) {
        ++cnt;
        if (cnt * fact[pos - i] >= K) {
          flag[j] = true;
          perm[i] = N - pos + j;
          K -= (cnt - 1) * fact[pos - i];
          break;
        }
      }
  }
  for (i = 1; i <= pos; ++i)
    if (isLucky(N - pos + i) && isLucky(perm[i])) ++sol;
  return sol;
}
int main() {
  scanf("%d %d", &N, &K);
  cout << solve() << endl;
  return 0;
}
