#include <bits/stdc++.h>
using namespace std;
const signed long long Infinity = 1000000001;
const long double Pi = 2.0L * asinl(1.0L);
const long double Epsilon = 0.000000001;
template <class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
  return os << "(" << p.first << "," << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& os, const vector<T>& V) {
  for (typeof(V.begin()) i = V.begin(); i != V.end(); ++i) os << *i << " ";
  return os;
}
template <class T>
ostream& operator<<(ostream& os, const set<T>& S) {
  for (typeof(S.begin()) i = S.begin(); i != S.end(); ++i) os << *i << " ";
  return os;
}
template <class T, class U>
ostream& operator<<(ostream& os, const map<T, U>& M) {
  for (typeof(M.begin()) i = M.begin(); i != M.end(); ++i) os << *i << " ";
  return os;
}
vector<signed long long> S;
void findLuckies() {
  for (int(k) = (1); (k) < (12); (k)++)
    for (int(i) = (0); (i) < (1 << k); (i)++) {
      signed long long t = i;
      signed long long r = 0;
      for (int(j) = (1); (j) <= (k); (j)++) {
        if (t % 2 == 1) {
          r *= 10;
          r += 7;
        } else {
          r *= 10;
          r += 4;
        }
        t /= 2;
      }
      S.push_back(r);
    }
  sort(S.begin(), S.end());
}
signed long long factorial(signed long long n) {
  if (n > 20) return Infinity * Infinity;
  signed long long result = 1;
  for (int(i) = (1); (i) <= (n); (i)++) result *= i;
  return result;
}
vector<signed long long> kThPermutation(vector<signed long long> A, int k) {
  k--;
  vector<signed long long> R;
  int n = A.size();
  for (int(i) = (n); (i) >= (2); (i)--) {
    int t = i;
    signed long long q = factorial(i - 1);
    t = k / q;
    k -= q * t;
    R.push_back(A[t]);
    A.erase(A.begin() + t);
  }
  R.push_back(A.front());
  return R;
}
const int Q = 20;
bool isLucky(signed long long q) {
  return binary_search(S.begin(), S.end(), q);
}
int result() {
  signed long long n, k;
  cin >> n >> k;
  if (k > factorial(n))
    return -1;
  else {
    vector<signed long long> T;
    signed long long result = 0;
    int i = 0;
    while (S[i] < n - Q) {
      result++;
      i++;
    }
    int w;
    if (i > 0)
      w = S[i - 1] + 1;
    else
      w = 0;
    int b = max(n - Q, (signed long long)w);
    for (int(j) = (b); (j) <= (n); (j)++) {
      T.push_back(j);
    }
    T = kThPermutation(T, k);
    for (int(j) = (0); (j) < (T.size()); (j)++)
      if (isLucky(j + b) and isLucky(T[j])) result++;
    return result;
  }
}
int main() {
  findLuckies();
  cout << result();
  return 0;
}
