#include <bits/stdc++.h>
using namespace std;
int countbit(int n) { return (n == 0) ? 0 : (1 + countbit(n & (n - 1))); }
int lowbit(int n) { return (n ^ (n - 1)) & n; }
const double pi = acos(-1.0);
const double eps = 1e-11;
long long p[20];
int N, K;
vector<int> path;
vector<long long> lucky;
int exist;
void proc(vector<int> seq, int size, int kth) {
  if (size == 0 || exist == 0) return;
  vector<int> next;
  int r = kth / p[size - 1];
  int m = kth % p[size - 1];
  if (r >= size) {
    exist = 0;
    return;
  }
  path.push_back(seq[r]);
  for (int i = 0; i < size; ++i)
    if (i != r) next.push_back(seq[i]);
  proc(next, size - 1, m);
}
void get(long long n) {
  if (n) lucky.push_back(n);
  if (n < 1000000000) {
    get(n * 10 + 4);
    get(n * 10 + 7);
  }
}
bool isLucky(int n) {
  while (n) {
    if (n % 10 != 4 && n % 10 != 7) return false;
    n /= 10;
  }
  return true;
}
int main() {
  p[0] = 1;
  for (int i = 1; i <= 15; ++i) p[i] = i * p[i - 1];
  cin >> N >> K;
  vector<int> seq;
  int size;
  if (N < 15)
    size = N;
  else
    size = 15;
  for (int i = 0; i < size; ++i) seq.push_back(N - i);
  sort(seq.begin(), seq.end());
  K--;
  exist = 1;
  proc(seq, size, K);
  get(0);
  sort(lucky.begin(), lucky.end());
  int ans = 0;
  if (N < 15) {
    for (int i = 0; i < path.size(); ++i)
      if (isLucky(i + 1) && isLucky(path[i])) ans++;
  } else {
    int index;
    for (int i = 0; i < lucky.size(); ++i) {
      if (lucky[i] > N - 15) {
        ans += i;
        break;
      }
    }
    for (int i = 0; i < path.size(); ++i)
      if (isLucky(i + N - 14) && isLucky(path[i])) ans++;
  }
  printf("%d\n", exist ? ans : -1);
  return 0;
}
