#include <bits/stdc++.h>
using namespace std;
int N, num = 0, co = 0, tempo[16], tag[17];
long long temp[10001], K, dp[16], ans[16];
void DFS(long long now) {
  if (now > N) {
    temp[num++] = now;
    return;
  }
  temp[num++] = now;
  DFS(now * 10 + 4);
  DFS(now * 10 + 7);
}
int Check(long long key) {
  for (int i = 1; i < num; i++)
    if (temp[i] == key) return true;
  return false;
}
int main() {
  scanf("%d%I64d", &N, &K);
  K--;
  DFS(0);
  sort(temp, temp + num);
  dp[0] = 1;
  for (long long i = 1; dp[i - 1] * i <= K; i++) dp[i] = dp[i - 1] * i, co++;
  for (int i = co; i >= 0; i--) {
    long long x = (K / dp[i]);
    int gg = 0;
    for (int j = 0; j <= x; j++)
      if (tag[j] == 1) gg++;
    while (gg > 0) {
      x++;
      if (tag[x] == 1) gg++;
      gg--;
    }
    tag[x] = 1;
    ans[i] = x;
    K -= (K / dp[i]) * dp[i];
  }
  if (co >= N)
    printf("-1\n");
  else {
    int show = 0, run = 1;
    for (; run < num && temp[run] <= N - co - 1; run++) show++;
    run--;
    for (int i = co; i >= 0; i--) {
      if (Check(N - i) && Check(ans[i] + 1 + (N - co - 1))) show++;
    }
    printf("%d\n", show);
  }
  return 0;
}
