#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 15 + 5;
long long fact[MAX_N] = {1};
bool mark[MAX_N];
int a[MAX_N];
void Permutation(int k, int id) {
  if (id == 15) return;
  int x = (k - 1) / fact[14 - id];
  k -= x * fact[14 - id];
  for (int i = 0; i < 15; i++) {
    if (!mark[i] && x)
      x--;
    else if (!mark[i]) {
      a[id] = i, mark[i] = true;
      return Permutation(k, id + 1);
    }
  }
}
bool isLucky(int n) {
  return n ? (n % 10 == 4 || n % 10 == 7) && isLucky(n / 10) : true;
}
int countLucky(int n, long long num = 0) {
  return num <= n ? (num > 0) + countLucky(n, num * 10 + 4) +
                        countLucky(n, num * 10 + 7)
                  : 0;
}
int main() {
  ios_base ::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  int n, k;
  cin >> n >> k;
  for (int i = 1; i < MAX_N; i++) fact[i] = fact[i - 1] * i;
  if (n < MAX_N && k > fact[n]) return cout << "-1\n", 0;
  Permutation(k, 0);
  int tmp = n, ans = 0;
  for (int i = 0; i < 15; i++)
    if (a[i] != i) {
      tmp -= 15 - i;
      for (int j = i; j < 15; j++)
        ans += (isLucky(tmp + j - i + 1) && isLucky(tmp + a[j] - i + 1));
      break;
    }
  cout << ans + countLucky(tmp) << endl;
  return 0;
}
