#include <bits/stdc++.h>
using namespace std;
long long jc[15];
int da[15];
void sjr(int a, long long b, long long c) {
  sort(da + a, da + a + c);
  if (c == 1) return;
  if (b <= jc[c - 1]) sjr(a + 1, b, c - 1);
  for (long long i = 1; i <= c; i++) {
    if (b <= i * jc[c - 1]) {
      swap(da[a], da[a + i - 1]);
      b -= (i - 1) * jc[c - 1];
      sjr(a + 1, b, c - 1);
      break;
    }
  }
}
bool ju(int a) {
  char cc[100];
  sprintf(cc, "%d", a);
  for (int i = 0; i < strlen(cc); i++)
    if (cc[i] != '4' && cc[i] != '7') return false;
  return true;
}
int main() {
  vector<unsigned long long> vd;
  for (int i = 1; i <= 10; i++) {
    for (int j = 0; j < (1 << i); j++) {
      unsigned long long ta = 0;
      for (int k = 0; k < i; k++) {
        if (j & (1 << k))
          ta += pow(10LL, k * 1LL) * 7LL;
        else
          ta += pow(10LL, k * 1LL) * 4LL;
      }
      vd.push_back(ta);
    }
  }
  jc[0] = 1;
  jc[1] = 1;
  for (long long i = 2; i <= 14; i++) jc[i] = jc[i - 1] * i;
  long long n, k;
  cin >> n >> k;
  if (n <= 14 && k > jc[n]) {
    printf("-1\n");
    return 0;
  }
  int j = 13;
  int bh = n;
  int fz = 0;
  for (int i = 0; i < 13 && n - i > 0; i++) {
    da[--j] = n - i;
    fz++;
    bh--;
  }
  sjr(j, k, fz);
  int jx = 0;
  for (int i = 0; i < vd.size(); i++) {
    if (vd[i] <= bh) jx++;
  }
  for (bh++; bh <= n; bh++) {
    if (ju(da[j]) && ju(bh)) jx++;
    j++;
  }
  cout << jx << endl;
  return 0;
}
