#include <bits/stdc++.h>
using namespace std;
queue<long long> q;
const long long INF = 1ll << 60, INF2 = 1ll << 58;
vector<long long> lucky;
long long cur = 0;
vector<pair<long long, long long> > a;
vector<pair<long long, char> > b, bb;
long long lft[528000], rght[528000];
int main() {
  int n;
  long long k, minsize = INF;
  cin >> n >> k;
  a.resize(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i].first >> a[i].second,
        minsize = min(minsize, a[i].second - a[i].first);
  lucky.reserve(529000);
  q.push(4), q.push(7);
  while (!q.empty()) {
    long long x = q.front();
    q.pop();
    lucky.push_back(x);
    if (x > INF2) continue;
    q.push(x * 10ll + 4ll), q.push(x * 10ll + 7ll);
  }
  for (int i = 0; i < lucky.size(); ++i) lft[i] = INF, rght[i] = INF;
  b.reserve(630000);
  bb.reserve(630000);
  for (int i = 0; i < lucky.size(); ++i) bb.push_back(make_pair(lucky[i], 1));
  b = bb;
  for (int i = 0; i < n; ++i) b.push_back(make_pair(a[i].second, 0));
  sort(b.begin() + lucky.size(), b.end());
  inplace_merge(b.begin(), b.begin() + lucky.size(), b.end());
  long long cnt = 0, cost = 0;
  int lcnt = 0;
  if (!b[0].second)
    cnt++;
  else
    lcnt++, lft[0] = 0;
  for (int i = 1; i < b.size(); ++i) {
    cost += cnt * (b[i].first - b[i - 1].first);
    if (cost > k || cost < 0) break;
    if (b[i].second)
      lft[lcnt++] = cost;
    else
      cnt++;
  }
  b = bb;
  for (int i = 0; i < n; ++i) b.push_back(make_pair(a[i].first, 0));
  sort(b.begin() + lucky.size(), b.end());
  inplace_merge(b.begin(), b.begin() + lucky.size(), b.end());
  reverse(b.begin(), b.end());
  lcnt = lucky.size() - 1, cnt = 0, cost = 0;
  for (int i = 1; i < b.size(); ++i) {
    cost += cnt * (b[i - 1].first - b[i].first);
    if (cost > k || cost < 0) break;
    if (b[i].second)
      rght[--lcnt] = cost;
    else
      cnt++;
  }
  int r = 0, ans = 0;
  for (int i = 0; i < lucky.size(); ++i) {
    r = max(i, r);
    while (r < lucky.size() && lucky[r] - lucky[i] <= minsize &&
           lft[r] + rght[i] <= k) {
      ans = max(ans, r - i + 1);
      r++;
    }
  }
  cout << ans;
  return 0;
}
