#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int n, o, ct, lt, ans;
long long cs, k, lc, rc, mini;
long long a[maxn * 7];
int b[20];
pair<long long, long long> seg[maxn];
unsigned long long sum1[maxn], db1 = 0, dc3 = 0;
double sum2[maxn], db2 = 0;
priority_queue<long long> sp;
bool ok(long long l, long long r) {
  if (sum2[lt] - double(l) * (n - lt + 1) > k * 6) return false;
  unsigned long long e = sum1[lt] - l * (n - lt + 1);
  if (e > k) return false;
  while ((!sp.empty()) && (-sp.top() <= r)) {
    db1 += -sp.top();
    db2 += -sp.top();
    ++dc3;
    sp.pop();
  }
  if (double(r) * dc3 - db2 > k * 6) return false;
  e += r * dc3 - db1;
  if (e <= k)
    return true;
  else
    return false;
}
int main() {
  o = 0;
  for (int i = 1; i <= 18; i++) {
    memset(b, 0, sizeof(b));
    while (b[0] == 0) {
      cs = 0;
      for (int j = 1; j <= i; j++)
        if (b[j] == 0)
          cs = cs * 10 + 4;
        else
          cs = cs * 10 + 7;
      ++o, a[o] = cs;
      ct = i;
      while (b[ct] == 1) {
        b[ct] = 0, --ct;
      }
      b[ct] = 1;
    }
  }
  sort(a + 1, a + o + 1);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> lc >> rc;
    seg[i] = make_pair(lc, rc);
  }
  mini = seg[1].second - seg[1].first + 1;
  for (int i = 2; i <= n; i++)
    mini = min(mini, seg[i].second - seg[i].first + 1);
  sort(seg + 1, seg + n + 1);
  sum1[n + 1] = 0;
  for (int i = n; i >= 1; i--) sum1[i] = sum1[i + 1] + seg[i].first;
  sum2[n + 1] = 0;
  for (int i = n; i >= 1; i--) sum2[i] = sum2[i + 1] + seg[i].first;
  int right;
  ans = 0;
  right = 0;
  lt = 1;
  for (int i = 1; i <= o; i++) {
    while ((lt <= n) && (seg[lt].first < a[i])) sp.push(-seg[lt].second), ++lt;
    while (right + 1 < i) ++right;
    while ((right + 1 <= o) && (ok(a[i], a[right + 1])) &&
           (a[right + 1] - a[i] + 1 <= mini))
      ++right;
    ans = max(ans, right - i + 1);
  }
  cout << ans << endl;
  return 0;
}
