#include <bits/stdc++.h>
using namespace std;
long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}
long long n, k, p[1000005], cnt, L[1000005], R[1000005];
double sumL, sumR;
struct cmpL {
  bool operator()(long long x, long long y) {
    return L[x] == L[y] ? x < y : L[x] < L[y];
  }
};
struct cmpR {
  bool operator()(long long x, long long y) {
    return R[x] == R[y] ? x < y : R[x] < R[y];
  }
};
multiset<long long, cmpL> s1;
multiset<long long, cmpR> s2;
void dfs(long long x) {
  if (x > 1e18) return;
  if (x != 0) p[++cnt] = x;
  dfs(x * 10 + 4);
  dfs(x * 10 + 7);
}
long long calc(long long ll, long long rr) {
  double right = s1.size(), left = n - s1.size() - s2.size();
  return (sumR - right * ll + left * rr - sumL) <= k;
}
void write(long long x) {
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
signed main() {
  n = read();
  k = read();
  dfs(0);
  sort(p + 1, p + 1 + cnt);
  long long minn = 1e18, ans = 0;
  long long j = 0;
  for (long long i = 1; i <= n; i++) {
    L[i] = read();
    R[i] = read();
    minn = min(minn, R[i] - L[i] + 1);
    sumR += L[i];
    s1.insert(i);
  }
  for (long long i = 1; i <= cnt; i++) {
    while (p[j + 1] - p[i] + 1 <= minn && j < cnt) {
      j++;
      while (s1.size()) {
        long long c = *s1.begin();
        if (L[c] > p[j]) break;
        s1.erase(c);
        sumR -= L[c];
        s2.insert(c);
      }
      while (s2.size()) {
        long long c = *s2.begin();
        if (R[c] >= p[j]) break;
        s2.erase(c);
        sumL += R[c];
      }
      if (calc(p[i], p[j])) {
        ans = max(ans, j - i + 1);
      } else
        break;
      ;
    }
  }
  write(ans);
}
