#include <bits/stdc++.h>
const int N = 100010;
const int inf = 0x3f3f3f3f;
using namespace std;
long long dp[N];
int used[21];
bool vis[21];
void dfs(int n, long long k, int cnt) {
  if (n == 0) return;
  int id = 1;
  for (int i = 1; i < n; i++)
    if (k > dp[n - 1])
      k -= dp[n - 1], id = i + 1;
    else
      break;
  for (int i = 1, j = 0; i < 20; i++) {
    if (vis[i]) continue;
    j++;
    if (j == id) vis[i] = 1, used[cnt] = i;
    if (j == id) break;
  }
  dfs(n - 1, k, cnt + 1);
}
int main() {
  long long n, k;
  cin >> n >> k;
  vector<long long> vt;
  set<long long> st;
  for (int k = 1; k < 11; k++) {
    for (long long i = 0; i < 1ll << k; i++) {
      long long cnt = 0;
      for (int j = 0; j < k; j++)
        if (i & (1ll << j))
          cnt = cnt * 10 + 7;
        else
          cnt = cnt * 10 + 4;
      vt.push_back(cnt);
      st.insert(cnt);
    }
  }
  sort(vt.begin(), vt.end());
  dp[0] = 1;
  for (int i = 1; i < 20; i++) dp[i] = i * dp[i - 1];
  if (n <= 19 && dp[n] < k) {
    puts("-1");
    return 0;
  }
  dfs(min(n, 19ll), k, 1);
  long long ret = 0;
  if (n <= 19) {
    ret = 0;
    if (used[4] == 4 || used[4] == 7) ret++;
    if (used[7] == 7 || used[7] == 4) ret++;
  } else {
    ret = vt.size() - (vt.end() - upper_bound(vt.begin(), vt.end(), n - 19));
    for (int i = 1; i <= 19; i++)
      if (st.count(used[i] + n - 19) && st.count(n - 19 + i)) ret++;
  }
  cout << ret << endl;
  return 0;
}
