#include <bits/stdc++.h>
using namespace std;
void file_open() {
  freopen("test.txt", "rt", stdin);
  freopen("test.out", "wt", stdout);
}
bool check(unsigned long long x) {
  while (x) {
    if (x % 10 != 7 && x % 10 != 4) return false;
    x /= 10;
  }
  return true;
}
void swap(int &a, int &b) {
  int tmp = a;
  a = b;
  b = tmp;
}
int main() {
  unsigned long long gt[20] = {}, a[20];
  queue<int> qu;
  qu.push(4);
  qu.push(7);
  unsigned long long n, k, t, dem, tmp, tmp2;
  cin >> n >> k;
  t = 1;
  gt[t] = 1;
  gt[0] = 1;
  while (gt[t] < k) {
    ++t;
    gt[t] = gt[t - 1] * t;
  }
  if (t > n) {
    printf("-1");
    return 0;
  }
  for (int i = t; i >= 1; --i) a[i] = n - t + i;
  dem = 0;
  tmp = 4;
  tmp2 = 1;
  while (tmp <= n) {
    if (qu.empty()) break;
    tmp = qu.front();
    if (tmp > n) break;
    qu.pop();
    if (tmp * 10 + 4 <= n) qu.push(tmp * 10 + 4);
    if (tmp * 10 + 7 <= n) qu.push(tmp * 10 + 7);
    if (n - tmp >= t)
      ++dem;
    else {
      while (n - tmp < t) {
        --t;
        int i = tmp2;
        while (k > gt[t]) {
          k -= gt[t];
          ++i;
          swap(a[i], a[tmp2]);
        }
        tmp2++;
      }
      if (check(a[tmp2 - 1])) ++dem;
    }
  }
  cout << dem;
  return 0;
}
