#include <bits/stdc++.h>
using namespace std;
const int MAXL = 21;
int n, k;
int seq[MAXL], bound;
int used[MAXL];
int seq_cal() {
  bound = 1;
  long long tot = 1;
  for (; tot < k; ++bound, tot = tot * bound)
    ;
  if (bound > n) return 0;
  memset(used, 0, sizeof(used));
  for (int num, i = bound; i > 0; --i) {
    tot /= i;
    seq[i] = (k - 1) / tot + 1;
    k = (k - 1) % tot + 1;
    num = seq[i];
    for (int j = 1; j <= bound; ++j)
      if (!used[j] && --num == 0) {
        used[j] = 1;
        seq[i] = (long long)j + n - bound;
      }
  }
  return 1;
}
int check_num(long long num) {
  num = n - num + 1;
  if (num <= bound)
    num = seq[num];
  else
    num = n - num + 1;
  for (; num; num /= 10)
    if (num % 10 != 4 && num % 10 != 7) return 0;
  return 1;
}
void solve() {
  if (!seq_cal()) {
    puts("-1");
    return;
  }
  int tot = 0;
  long long cur;
  for (int i = 1; i <= 10; ++i)
    for (int limit = (1 << i), j = 0; j < limit; ++j) {
      cur = 0;
      for (int l = i - 1; l >= 0; --l) {
        cur *= 10;
        if (j & (1 << l))
          cur += 7;
        else
          cur += 4;
      }
      if (cur > n) {
        printf("%d\n", tot);
        return;
      }
      if (check_num(cur)) ++tot;
    }
}
int main() {
  scanf("%d%d", &n, &k);
  solve();
  return 0;
}
