#include <bits/stdc++.h>
using namespace std;
const int Len = 2333333;
char buf[Len], *p1 = buf, *p2 = buf, duf[Len], *q1 = duf;
inline char gc();
inline int rd();
inline void pc(char c);
inline void rt(int x);
inline void flush();
int n, k, t, K, ans, f[15], p[15];
vector<int> V;
inline int check(int x) {
  while (x)
    if (x % 10 != 4 && x % 10 != 7)
      return 0;
    else
      x /= 10;
  return 1;
}
inline void Dfs(int x, int d) {
  if (x > 9 || d > n) return;
  if (x)
    if (d < n - t)
      ++ans;
    else if (check(p[n - d]))
      ++ans;
  Dfs(x + 1, d * 10 + 4), Dfs(x + 1, d * 10 + 7);
}
int main() {
  n = rd(), K = k = rd(), f[0] = 1;
  for (register int i = 1; i <= 12; ++i) f[i] = f[i - 1] * i;
  if (n < 13 && k > f[n]) return printf("-1"), 0;
  while (k >= f[t] && t < 13) ++t;
  --t, --k;
  for (register int i = n - t; i <= n; ++i) V.push_back(i);
  for (register int i = t; i >= 0; --i) {
    int a = k / f[i], b = k % f[i];
    k = b, p[i] = V[a], V.erase(V.begin() + a);
  }
  Dfs(0, 0), rt(ans);
  return flush(), 0;
}
inline char gc() {
  return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, Len, stdin), p1 == p2)
             ? -1
             : *p1++;
}
inline int rd() {
  char c;
  int f = 1;
  while (!isdigit(c = gc()) && c != '-')
    ;
  c == '-' ? f = -1, c = gc() : 0;
  int x = c ^ 48;
  while (isdigit(c = gc())) x = ((x + (x << 2)) << 1) + (c ^ 48);
  return x * f;
}
inline void pc(char c) {
  q1 == duf + Len &&fwrite(q1 = duf, 1, Len, stdout), *q1++ = c;
}
inline void rt(int x) {
  x < 0 ? pc('-'), x = -x : 0, pc((x >= 10 ? rt(x / 10), x % 10 : x) + 48);
}
inline void flush() { fwrite(duf, 1, q1 - duf, stdout); }
