#include <bits/stdc++.h>
using namespace std;
inline bool isgood(int a) {
  while (a)
    if ((a % 10 != 4) && (a % 10 != 7))
      return false;
    else
      a /= 10;
  return true;
}
inline int countgood(int r) {
  int ans = 0, b[10];
  long long a = 0;
  for (int i = 0; i < 10; i++) b[i] = 0;
  for (;;) {
    int i = 0;
    while (b[i] == 7) {
      b[i] = 4;
      i++;
    }
    b[i] = (b[i] == 0) ? 4 : 7;
    for (i = 9, a = 0; i >= 0; i--) a = a * 10 + b[i];
    if (a > r) break;
    ans++;
  }
  return ans;
}
int f[14], v[14], t[14];
int main() {
  f[0] = 1;
  for (int i = 1; i <= 13; i++) f[i] = f[i - 1] * i;
  int n, k;
  cin >> n >> k;
  k--;
  if (n <= 13) {
    if (f[n] <= k) {
      cout << -1;
      return 0;
    }
    for (int i = 0; i < n; i++) v[i] = i + 1;
    for (int i = n; i; i--) {
      int j = k / f[i - 1];
      k %= f[i - 1];
      t[n - i + 1] = v[j];
      for (; j < i - 1; j++) v[j] = v[j + 1];
    }
    int ans = 0;
    for (int i = 1; i <= n; i++)
      if (isgood(i) && isgood(t[i])) ans++;
    cout << ans;
    return 0;
  }
  for (int i = 0; i < 13; i++) v[i] = n - 12 + i;
  for (int i = 13; i; i--) {
    int j = k / f[i - 1];
    k %= f[i - 1];
    t[14 - i] = v[j];
    for (; j < i - 1; j++) v[j] = v[j + 1];
  }
  int ans = 0;
  for (int i = 1; i <= 13; i++)
    if (isgood(n - 13 + i) && isgood(t[i])) ans++;
  ans += countgood(n - 13);
  cout << ans;
  return 0;
}
