#include <bits/stdc++.h>
using namespace std;
const long long INF = 1ll << 60, INF2 = 1ll << 58;
queue<long long> q;
vector<long long> ll, rr, g;
long long lft[525000], rght[525000];
int main() {
  int n, r = 0, ans = 0;
  long long k, len = INF;
  scanf("%d %I64d", &n, &k);
  ll.resize(n), rr.resize(n);
  for (int i = 0; i < n; ++i) {
    scanf("%I64d %I64d", &ll[i], &rr[i]);
    len = min(len, rr[i] - ll[i]);
  }
  g.reserve(525000);
  q.push(4), q.push(7);
  while (!q.empty()) {
    long long x = q.front();
    q.pop();
    g.push_back(x);
    if (x > INF2) continue;
    q.push(x * 10ll + 4ll), q.push(x * 10ll + 7ll);
  }
  for (int i = 0; i < g.size(); ++i) lft[i] = INF, rght[i] = INF;
  sort(rr.begin(), rr.end());
  sort(ll.rbegin(), ll.rend());
  long long cnt = 0, cost = 0, last = 4ll;
  for (int i = 0; i < g.size() && cost <= k; ++i) {
    cost += cnt * (g[i] - last);
    while (cnt < n && rr[cnt] <= g[i]) cost += g[i] - rr[cnt], cnt++;
    if (cost > k || cost < 0) break;
    lft[i] = cost;
    last = g[i];
  }
  cost = 0, cnt = 0, last = g.back();
  for (int i = g.size() - 1; i >= 0; --i) {
    cost += cnt * (last - g[i]);
    while (cnt < n && ll[cnt] >= g[i]) cost += ll[cnt] - g[i], cnt++;
    if (cost > k || cost < 0) break;
    rght[i] = cost;
    last = g[i];
  }
  for (int i = 0; i < g.size(); ++i) {
    r = max(i, r);
    while (r < g.size() && g[r] - g[i] <= len && lft[r] + rght[i] <= k)
      ans = max(ans, r - i + 1), r++;
  }
  printf("%d", ans);
  return 0;
}
