#include <bits/stdc++.h>
using namespace std;
int N;
long long K;
long long F[12];
vector<long long> S;
void DFS(int a) {
  if (a > 10) {
    long long res = 0, flag = 0;
    for (int i = 1; i <= 10; i++) {
      if (F[i] == 0) {
        if (flag) return;
      } else if (F[i] == 1) {
        res = res * 10 + 4;
        flag = 1;
      } else
        res = res * 10 + 7, flag = 1;
    }
    S.push_back(res);
    return;
  }
  F[a] = 0;
  DFS(a + 1);
  F[a] = 1;
  DFS(a + 1);
  F[a] = 2;
  DFS(a + 1);
}
int C[200], A[200];
long long P[200];
int check(int a) {
  while (a > 0) {
    if (a % 10 != 7 && a % 10 != 4) return 0;
    a /= 10;
  }
  return 1;
}
int main() {
  DFS(1);
  scanf("%d%lld", &N, &K);
  long long pow = 1;
  bool flag = 0;
  for (int i = 1; i <= N; i++)
    if (pow * i >= K) {
      flag = 1;
      break;
    } else
      pow *= i;
  if (!flag) {
    puts("-1");
    return 0;
  }
  P[1] = P[0] = 1;
  for (int i = 2; P[i - 1] < 10000000000LL; i++) P[i] = P[i - 1] * (long long)i;
  pow = 1;
  int start;
  for (int i = 1; i <= N; i++) {
    if (pow * (long long)i >= K) {
      start = N - i + 1;
      break;
    }
    pow *= (long long)i;
  }
  int nn = N - start + 1;
  for (int i = 1; i <= nn; i++) {
    for (int j = nn; j >= 1; j--)
      if (!C[j]) {
        int x = 0;
        for (int k = 1; k < j; k++)
          if (!C[k]) x++;
        if (x * P[nn - i] < K) {
          C[j] = 1;
          A[i] = j;
          K -= (long long)x * P[nn - i];
          break;
        }
      }
  }
  for (int i = 1; i <= nn; i++)
    if (!C[i]) A[nn] = i;
  for (int i = 1; i <= nn; i++) A[i] += start - 1;
  sort(S.begin(), S.end());
  int cnt = 0;
  for (int i = 1; i < S.size(); i++) {
    if (start <= S[i]) break;
    cnt++;
  }
  for (int i = 1; i <= nn; i++) {
    if (check(A[i]) && check(start + i - 1)) cnt++;
  }
  printf("%d\n", cnt);
  return 0;
}
