#include <bits/stdc++.h>
using namespace std;
int lucky[3000], cnt;
void dfs(long long now) {
  if (now > 1000000000) return;
  if (now) lucky[cnt++] = (int)now;
  dfs(now * 10 + 4);
  dfs(now * 10 + 7);
}
long long fact[20];
int n, m, num[20];
bool flag[20];
int find(int k) {
  for (int i = 1;; i++) {
    if (!flag[i]) {
      k--;
      if (k == 0) return i;
    }
  }
}
void sol(int n, int m) {
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      m -= max(1ll, fact[n - i]);
      if (m <= 0) {
        num[i] = find(j);
        flag[num[i]] = true;
        m += fact[n - i];
        break;
      }
    }
  }
}
bool ck(int i) {
  while (i) {
    if (i % 10 != 4 && i % 10 != 7) return false;
    i /= 10;
  }
  return true;
}
int main() {
  cnt = 0;
  dfs(0);
  fact[0] = 1;
  for (int i = 1; i <= 15; i++) fact[i] = fact[i - 1] * i;
  fact[0] = 0;
  scanf("%d%d", &n, &m);
  if (n <= 15 && fact[n] < m) {
    printf("-1\n");
    return 0;
  }
  for (int i = max(1, n - 14); i <= n; i++) {
    if (fact[n - i + 1] >= m && fact[n - i] < m) {
      sol(n - i + 1, m);
      int ans = 0;
      for (int j = 0; j < cnt; j++)
        if (lucky[j] < i) ans++;
      for (int j = i; j <= n; j++) {
        if (ck(j) && ck(num[j - i + 1] + i - 1)) ans++;
      }
      printf("%d\n", ans);
      break;
    }
  }
  return 0;
}
