#include <bits/stdc++.h>
using namespace std;
const int N = 2005;
long long is_luck[N];
long long permu[20];
int get_luck();
int get_permu(long long, long long, long long, long long);
bool judge_luck(long long);
int main() {
  long long n, m, res = 0;
  cin >> n >> m;
  int cnt = 0;
  cnt = get_luck();
  long long remain = 1, data = 1;
  while (data < m) {
    data *= (remain++);
  }
  remain--;
  if (remain > n) {
    puts("-1");
    return 0;
  }
  long long end = n - remain;
  for (int i = 0; i < cnt; i++) {
    if (is_luck[i] <= end)
      res++;
    else
      break;
  }
  cnt = get_permu(remain, end + 1, m - 1, data);
  for (int i = 0; i < cnt; i++) {
    if (judge_luck(permu[i]) && judge_luck(i + end + 1)) res++;
  }
  cout << res << endl;
  return 0;
}
int get_luck() {
  int cnt = 0;
  is_luck[cnt++] = 4;
  is_luck[cnt++] = 7;
  for (int i = 0; i < cnt; i++) {
    if (is_luck[cnt - 1] > (1e9))
      break;
    else {
      is_luck[cnt++] = (is_luck[i] * 10 + 4);
      is_luck[cnt++] = (is_luck[i] * 10 + 7);
    }
  }
  return cnt;
}
int get_permu(long long num, long long begin, long long m, long long data) {
  int cnt = 0;
  set<long long> choice;
  for (int i = 0; i < num; i++) choice.insert(begin + i);
  for (int i = num; i > 0; i--) {
    data /= i;
    int pos = m / (data);
    m -= (pos) * (data);
    set<long long>::iterator it = choice.begin();
    while (it != choice.end() && pos) {
      it++;
      pos--;
    };
    permu[cnt++] = (*it);
    choice.erase(it);
  }
  return cnt;
}
bool judge_luck(long long n) {
  while (n) {
    if (n % 10 != 4 && n % 10 != 7)
      return false;
    else
      n /= 10;
  }
  return true;
}
