#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T pow2(T a) {
  return a * a;
}
template <class T>
inline bool mineq(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
vector<long long> luck;
long long fact[20];
bool islucky(long long n) {
  while (n) {
    if (n % 10 != 4 && n % 10 != 7) return false;
    n /= 10;
  }
  return true;
}
long long asf(long long n, long long k, long long pref) {
  bool used[20] = {};
  long long ans = 0;
  if (fact[n] < k) return -1;
  for (int i = 0; i < n; i++) {
    for (int j = 1; j < n + 1; j++)
      if (!used[j]) {
        if (fact[n - i - 1] < k) {
          k -= fact[n - i - 1];
        } else {
          used[j] = true;
          if (islucky(pref + i + 1) && islucky(pref + j)) ans++;
          break;
        }
      }
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  luck.push_back(4);
  luck.push_back(7);
  int m = 0;
  for (int i = 0; i < 10; i++) {
    int n = luck.size();
    for (int j = m; j < n; j++) {
      luck.push_back(luck[j] * 10 + 4);
      luck.push_back(luck[j] * 10 + 7);
    }
    m = n;
  }
  sort(luck.begin(), luck.end());
  fact[0] = 1;
  for (int i = 1; i < 16; i++) fact[i] = i * fact[i - 1];
  long long n, k;
  cin >> n >> k;
  if (n <= 15) {
    cout << asf(n, k, 0);
  } else {
    long long t = asf(15, k, n - 15);
    if (t != -1) {
      int k = upper_bound(luck.begin(), luck.end(), (n - 15)) - luck.begin();
      cout << t + k;
    } else
      cout << t;
  }
}
