#include <bits/stdc++.h>
using namespace std;
long long i, j, k, n, m, t, f, x, y, cnt;
long long nl, tmp, gcnt, ans;
long long luckyNum[20007], fct[16], v[16], ad[16];
queue<long long> qa, qb;
set<long long> st;
long long findFct(long long n) {
  i = 0;
  for (i = 1; i < 16; i++) {
    if (fct[i] > n) {
      return i;
    }
  }
}
void setV(long long n) {
  long long i;
  n++;
  for (i = 1; i < 16; i++) {
    if (!v[i]) {
      n--;
    }
    if (n == 0) {
      v[i] = true;
      gcnt++;
      ad[gcnt] = i + nl;
      break;
    }
  }
}
int main() {
  memset(luckyNum, 0, sizeof(luckyNum));
  cnt = 0;
  qa.push(4);
  luckyNum[cnt++] = 4;
  st.insert(4);
  qa.push(7);
  luckyNum[cnt++] = 7;
  st.insert(7);
  for (i = 1; i < 10; i++) {
    while (!qa.empty()) {
      x = qa.front();
      qa.pop();
      qb.push(x * 10 + 4);
      luckyNum[cnt++] = x * 10 + 4;
      st.insert(x * 10 + 4);
      qb.push(x * 10 + 7);
      luckyNum[cnt++] = x * 10 + 7;
      st.insert(x * 10 + 7);
    }
    while (!qb.empty()) {
      qa.push(qb.front());
      qb.pop();
    }
  }
  fct[0] = 1;
  for (i = 1; i < 16; i++) {
    fct[i] = fct[i - 1] * i;
  }
  while (cin >> n >> k) {
    memset(v, 0, sizeof(v));
    memset(ad, 0, sizeof(ad));
    gcnt = 0;
    ans = 0;
    k--;
    m = findFct(k);
    nl = n - m;
    if (nl < 0) {
      cout << "-1" << endl;
      continue;
    }
    i = 0;
    while (luckyNum[i] <= nl) {
      ans++;
      i++;
    }
    tmp = m;
    m--;
    while (k > 0) {
      t = k / fct[m];
      k %= fct[m];
      m--;
      setV(t);
    }
    for (i = 1; i <= tmp; i++) {
      if (!v[i]) {
        gcnt++;
        ad[gcnt] = i + nl;
      }
    }
    for (i = 1; i <= gcnt; i++) {
      if (st.find(ad[i]) != st.end() && st.find(i + nl) != st.end()) {
        ans++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
