#include <bits/stdc++.h>
using namespace std;
int bound[15];
int n, k, ans;
long long lucky[3000];
int num;
void dfs(long long x, int pos) {
  long long c1 = x * 10ll + 4ll, c2 = x * 10ll + 7ll;
  if (pos <= 10) {
    lucky[num++] = c1;
    lucky[num++] = c2;
    dfs(c1, pos + 1);
    dfs(c2, pos + 1);
  }
}
int main() {
  lucky[num++] = 0;
  dfs(0, 1);
  sort(lucky, lucky + num);
  bound[0] = 0;
  bound[1] = 1;
  for (int i = 2; i <= 12; ++i) bound[i] = bound[i - 1] * i;
  ans = 0;
  scanf("%d%d", &n, &k);
  if (n <= 12 && k > bound[n])
    puts("-1");
  else {
    int st = (((1) > (n - 12)) ? (1) : (n - 12));
    bool used[20];
    memset(used, 0, sizeof(used));
    ans = lower_bound(lucky, lucky + num, st) - lucky - 1;
    int c = 0;
    for (int i = (((12) < (n - 1)) ? (12) : (n - 1)); i >= 1; --i) {
      while (k > bound[i]) {
        ++c;
        k -= bound[i];
      }
      int count = -1;
      for (int j = 0; j <= (((12) < (n)) ? (12) : (n)); ++j) {
        if (!used[j]) ++count;
        if (count == c) {
          used[j] = 1;
          int tmp = lower_bound(lucky, lucky + num, st + j) - lucky;
          int tmp2 = lower_bound(lucky, lucky + num, n - i) - lucky;
          if (lucky[tmp] == st + j && lucky[tmp2] == n - i) ++ans;
          break;
        }
      }
      c = 0;
    }
    for (int i = 0; i <= (((12) < (n)) ? (12) : (n)); ++i) {
      if (!used[i]) {
        int tmp = lower_bound(lucky, lucky + num, st + i) - lucky;
        int tmp2 = lower_bound(lucky, lucky + num, n) - lucky;
        if (lucky[tmp] == st + i && lucky[tmp2] == n) ++ans;
        break;
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}
