#include <bits/stdc++.h>
using namespace std;
set<long long> Luckys, adads;
long long n, k, fact = 1, f = 1, facts[100], ans;
bool mark[100];
vector<int> Permutation;
bool isLucky(long long n) {
  while (n) {
    if (n % 10 != 4 && n % 10 != 7) return 0;
    n /= 10;
  }
  return 1;
}
void Lucky_Make(long long n, long long MAX) {
  if (n > MAX) return;
  Luckys.insert(n);
  Lucky_Make(n * 10 + 4, MAX);
  Lucky_Make(n * 10 + 7, MAX);
}
void Calc_Permutation(int n, int k) {
  int ans = 0;
  for (int i = 1; i <= n; i++) adads.insert(i);
  for (int i = 1; i <= n; i++) {
    int now = k / facts[n - i], j;
    k %= facts[n - i];
    set<long long>::iterator it = adads.begin();
    for (j = 0; j < now; j++) it++;
    Permutation.push_back(*it);
    adads.erase(it);
  }
}
int Count(int f) {
  int ans = 0;
  for (int i = 0; i < Permutation.size(); i++)
    if (isLucky(Permutation[i] + n - f) && isLucky(i + 1 + n - f)) ans++;
  return ans;
}
int main() {
  cin >> n >> k;
  facts[0] = 1;
  for (; fact < k; f++) {
    fact *= f;
    facts[f] = fact;
  }
  f--;
  if (f > n) {
    cout << -1 << endl;
    return 0;
  }
  Lucky_Make(4, n - f);
  Lucky_Make(7, n - f);
  ans = Luckys.size();
  Calc_Permutation(f, k - 1);
  ans += Count(f);
  cout << ans << endl;
}
