#include <bits/stdc++.h>
using namespace std;
using namespace std;
const long long Max = 1100000000000000000LL;
struct Tnode {
  long long l, r;
};
long long a[600000];
long long s1[600000], s2[600000];
Tnode p[110000];
int tot, n;
long long m;
void Prepare(long long now, int x, int y) {
  if (x == y) {
    a[++tot] = now;
    return;
  }
  Prepare(now * 10 + (long long)4, x + 1, y);
  Prepare(now * 10 + (long long)7, x + 1, y);
}
bool Cmp1(Tnode A, Tnode B) { return A.r < B.r; }
bool Cmp2(Tnode A, Tnode B) { return A.l > B.l; }
void Add(long long &xx, long long yy) {
  xx += yy;
  if (xx >= Max) xx = Max;
}
long long Mul(long long xx, long long yy) {
  if (!xx || !yy) return 0;
  if (Max / xx < yy) return Max;
  return xx * yy;
}
int main() {
  tot = 0;
  for (int i = 1; i <= 18; ++i) Prepare(0, 0, i);
  scanf("%d%I64d", &n, &m);
  for (int i = 1; i <= n; ++i) scanf("%I64d%I64d", &p[i].l, &p[i].r);
  sort(p + 1, p + n + 1, Cmp1);
  int node = 1;
  memset(s1, 0, sizeof(s1));
  for (int i = 1; i <= tot; ++i) {
    long long D = Mul(a[i] - a[i - 1], node - 1);
    Add(s1[i], s1[i - 1] + D);
    while (p[node].r <= a[i] && node <= n) {
      Add(s1[i], a[i] - p[node].r);
      ++node;
    }
  }
  sort(p + 1, p + n + 1, Cmp2);
  memset(s2, 0, sizeof(s2));
  node = 1;
  for (int i = tot; i >= 1; --i) {
    long long D = Mul(a[i + 1] - a[i], node - 1);
    Add(s2[i], s2[i + 1] + D);
    while (p[node].l >= a[i] && node <= n) {
      Add(s2[i], p[node].l - a[i]);
      ++node;
    }
  }
  long long len = Max;
  for (int i = 1; i <= n; ++i) len = min(len, p[i].r - p[i].l);
  int ans = 0;
  for (int i = 1; i <= tot; ++i) {
    long long cost = s1[i] + s2[i];
    if (cost <= m) {
      cost = s1[i];
      int l, r;
      l = 1;
      r = i;
      while (l < r) {
        int mid = (l + r) / 2;
        if (a[i] - a[mid] > len)
          l = mid + 1;
        else
          r = mid;
      }
      r = i;
      while (l < r) {
        int mid = (l + r) / 2;
        if (cost + s2[mid] <= m)
          r = mid;
        else
          l = mid + 1;
      }
      if (i - l + 1 > ans) ans = i - l + 1;
    }
  }
  printf("%d\n", ans);
  return 0;
}
