#include <bits/stdc++.h>
using namespace std;
const long long LINF = ~(((long long)0x1) << 63) / 2;
const int INF = 0X3F3F3F3F;
const double eps = 1e-7;
const long long limit = 1000000000LL;
vector<long long> c;
void dfs(long long now) {
  if (now > limit) return;
  if (now != 0) c.push_back(now);
  dfs(now * 10 + 4);
  dfs(now * 10 + 7);
}
int loc[15], lim;
void solve(int pos, int k) {
  int now = 1, i, j, ans = 0, cnt = 0;
  if (pos == lim + 1) return;
  for (i = 1; i <= pos - 1; i++) now *= i;
  for (i = 1; i <= lim; i++)
    if (!loc[i]) {
      ans += now;
      cnt++;
      if (ans >= k) {
        loc[i] = lim - pos + 1;
        solve(pos - 1, k - now * (cnt - 1));
        break;
      }
    }
}
bool isOk(int d) {
  bool flag = true;
  while (d) {
    int c = d % 10;
    if (c != 4 && c != 7) flag = false;
    d /= 10;
  }
  return flag;
}
int main() {
  int n, k, i, j;
  cin >> n >> k;
  long long ans = 1;
  bool ok = false;
  dfs(0);
  sort(c.begin(), c.end());
  for (i = 1; i <= n; i++) {
    ans *= i;
    if (ans >= k) {
      ok = true;
      lim = i;
      break;
    }
  }
  if (!ok) {
    cout << "-1" << endl;
  } else {
    solve(lim, k);
    int ans = 0;
    for (i = 0; i < c.size() && c[i] <= n; i++)
      if (c[i] < n - lim)
        ans++;
      else {
        if (isOk(loc[(int)(c[i] - (n - lim))] + n - lim)) ans++;
      }
    cout << ans << endl;
  }
  return 0;
}
