#include <bits/stdc++.h>
using namespace std;
inline void read(register int *n) {
  register char c;
  *n = 0;
  do {
    c = getchar();
  } while (c < '0' || c > '9');
  do {
    *n = c - '0' + *n * 10;
    c = getchar();
  } while (c >= '0' && c <= '9');
}
vector<int> kperm(vector<int> V, long long k) {
  k--;
  vector<int> res;
  long long fac[20] = {1};
  for (register int i = (1); i < (20); ++i) fac[i] = i * fac[i - 1];
  if (k >= fac[((int)(V).size())]) {
    cout << -1 << "\n";
    exit(0);
  }
  while (!V.empty()) {
    long long f = fac[((int)(V).size()) - 1];
    res.push_back(V[k / f]);
    V.erase(V.begin() + k / f);
    k %= f;
  }
  return res;
}
vector<long long> V;
void gen(long long x) {
  if (x > 0) V.push_back(x);
  if (x > 1000LL * 1000 * 1000 * 100) return;
  gen(x * 10 + 7);
  gen(x * 10 + 4);
}
bool islucky(int x) {
  while (x % 10 == 7 || x % 10 == 4) x /= 10;
  return x == 0;
}
int main(int argv, char **argc) {
  gen(0);
  long long n, k, m;
  cin >> n >> k;
  m = min(16LL, n);
  vector<int> tmp;
  for (register int i = (0); i < (m); ++i) tmp.push_back(n - i);
  sort((tmp).begin(), (tmp).end());
  int cnt = 0;
  for (register int i = (0); i < (((int)(V).size())); ++i)
    if (V[i] <= n - 16) cnt++;
  tmp = kperm(tmp, k);
  for (register int i = (0); i < (m); ++i)
    if (islucky(tmp[((int)(tmp).size()) - 1 - i]) && islucky(n - i)) cnt++;
  cout << cnt << "\n";
  return 0;
}
