#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000007;
const int N = 100100;
int a[N], cnt, n, k;
long long F[111], sum;
const int IDX[] = {4, 7};
void DP(int id) {
  if (sum > n) return;
  if (sum) a[++cnt] = sum;
  for (int i = 0; i < 2; i++) {
    sum = sum * 10 + IDX[i];
    DP(i + 1);
    sum /= 10;
  }
}
int MAX, ans;
bool OK[N];
int main() {
  cin >> n >> k;
  DP(1);
  sort(a + 1, a + cnt + 1);
  F[0] = 1;
  for (int i = 1; i <= 20; i++) F[i] = F[i - 1] * i;
  if (n <= 20 && k > F[n]) {
    cout << "-1";
    return 0;
  }
  for (int i = 1; i <= 20; i++)
    if (F[i] >= k) {
      MAX = i;
      break;
    }
  memset(OK, true, sizeof(OK));
  for (int i = 1; i <= MAX; i++) {
    int j = 1;
    for (j = 1; j <= MAX; j++)
      if (OK[j])
        if (k > F[MAX - i])
          k -= F[MAX - i];
        else
          break;
    OK[j] = false;
    if (binary_search(a + 1, a + cnt + 1, n - MAX + i) &&
        binary_search(a + 1, a + cnt + 1, n - MAX + j))
      ans++;
  }
  for (int i = 1; i <= cnt; i++)
    if (a[i] <= n - MAX)
      ans++;
    else
      break;
  cout << ans;
}
