#include <bits/stdc++.h>
using namespace std;
long long jc[14];
long long ans[14];
int n, k;
int vis[14];
void f(long long k, int dep) {
  if (dep == 0) {
    return;
  }
  if (jc[dep - 1] > k) {
    for (int i = 1; i <= n; i++) {
      if (!vis[i]) {
        ans[min(n, 13) - dep] = i;
        vis[i] = 1;
        f(k, dep - 1);
        return;
      }
    }
  } else {
    int d = 1;
    while (dep != 1 && jc[dep - 1] * (d) <= k) {
      d++;
    }
    int ff = 0;
    int i;
    for (i = 1; i <= n; i++) {
      if (!vis[i]) {
        ff++;
      }
      if (ff == d) {
        vis[i] = 1;
        break;
      }
    }
    ans[min(n, 13) - dep] = i;
    f(k - jc[dep - 1] * (d - 1), dep - 1);
  }
}
bool is(int p) {
  if (p == 0) return false;
  while (p) {
    if (p % 10 != 4 && p % 10 != 7) return false;
    p /= 10;
  }
  return true;
}
int main(int argc, char** argv) {
  scanf("%d%d", &n, &k);
  jc[0] = 0;
  jc[1] = 1;
  int p = n, st = 1;
  for (int i = 2; i <= 13; i++) {
    jc[i] = jc[i - 1] * i;
  }
  k--;
  if (n <= 13 && jc[n] <= k) {
    cout << -1;
    return 0;
  }
  if (n > 13) {
    p = 13;
    st = n - 13 + 1;
  }
  f(k, min(13, n));
  int anss = 0;
  int qq = n - p, fl = 1;
  if (qq)
    for (int k = 1; k <= 9; k++) {
      for (int i = 0; i < (1 << k); i++) {
        long long g = 0;
        long long q = 1;
        for (int j = 0; j < k; j++) {
          if ((1 << j) & i) {
            g += 7 * q;
          } else {
            g += 4 * q;
          }
          q *= 10;
        }
        if (g > qq) {
          fl = 0;
          break;
        }
        if (g <= qq) anss++;
      }
      if (!fl) break;
    }
  for (int i = 0; i < p; i++) {
    if (is(ans[i] - 1 + st) && is(i + st)) {
      anss++;
    }
  }
  cout << anss;
  return 0;
}
