#include <bits/stdc++.h>
using namespace std;
int n, kk, ans, a[15];
long long ci[15];
bool ok;
bool check(int i) {
  while (i) {
    if ((i % 10 != 4) && (i % 10 != 7)) return false;
    i = i / 10;
  }
  return true;
}
int cal(int r) {
  long long now = 0;
  int ans = 0, a[11] = {0};
  while (now < r) {
    int j = 1;
    while ((j <= a[0]) && (a[j] == 7)) j++;
    if (j == a[0] + 1) {
      a[++a[0]] = 4;
      now += ci[j - 1] * 4;
    } else {
      a[j] = 7;
      now += ci[j - 1] * 3;
    }
    for (int i = (1); i <= (j - 1); i++) {
      a[i] = 4;
      now -= ci[i - 1] * 3;
    }
    if (now <= r) ans++;
  }
  return ans;
}
int main() {
  scanf("%d%d", &n, &kk);
  kk--;
  ci[0] = 1;
  for (int i = (1); i <= (9); i++) ci[i] = ci[i - 1] * 10;
  ans += cal(n - 13);
  for (int i = (max(n - 12, 1)); i <= (n); i++) a[++a[0]] = i;
  for (int i = (max(n - 12, 1)); i <= (n); i++) {
    int t = 1;
    ok = false;
    for (int j = (1); j <= (n - i); j++) t *= j;
    for (int k = (1); k <= (a[0]); k++)
      if (a[k])
        if (kk >= t)
          kk -= t;
        else {
          ans += check(i) && check(a[k]);
          a[k] = 0;
          ok = true;
          break;
        }
    if (!ok) break;
  }
  if (ok)
    printf("%d", ans);
  else
    printf("-1");
  return 0;
}
