#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7, INF = 1e18;
long long n, k;
long long tot(long long count) {
  if (count > n) return 0;
  return 1 + tot(count * 10 + 7) + tot(count * 10 + 4);
}
long long findit(long long a) {
  while (a) {
    if (a % 10 != 4 && a % 10 != 7) return 0;
    a /= 10;
  }
  return 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1;
  while (t--) {
    cin >> n >> k;
    vector<long long> vect(1, 1);
    long long i = 0, ans = tot(4) + tot(7);
    while (vect[i] < k) {
      vect.push_back(vect[i] * (i + 2));
      i++;
    }
    if (vect.size() > n) {
      cout << "-1\n";
      continue;
    }
    k--;
    vector<int> vis(vect.size(), 0), perm;
    long long m = n - vect.size() + 1, count = 0;
    while (k) {
      i = upper_bound(vect.begin(), vect.end(), k) - vect.begin();
      long long j = 0;
      while (count < vis.size() - i - 1) {
        while (vis[j]) j++;
        vis[j] = 1;
        count++;
        perm.push_back(j);
      }
      i--;
      long long idx = k / vect[i];
      k %= vect[i];
      while (idx) {
        while (vis[j]) j++;
        j++;
        idx--;
      }
      while (vis[j]) j++;
      vis[j] = 1;
      count++;
      perm.push_back(j);
    }
    for (i = 0; i < vis.size(); i++)
      if (!vis[i]) {
        perm.push_back(i);
      }
    for (i = 0; i < perm.size(); i++) {
      if (findit(perm[i] + m)) {
        if (!findit(i + m)) {
          ans--;
        }
      }
    }
    cout << ans << "\n";
  }
  return 0;
}
