#include <bits/stdc++.h>
using namespace std;
const long long INF = 1ll << 60;
struct seg {
  long long l, r;
  seg(){};
  seg(long long _l, long long _r) : l(_l), r(_r) {}
};
struct event {
  long long x;
  bool lucky;
  event() { lucky = false; }
  event(long long _x, bool _lucky) : x(_x), lucky(_lucky) {}
};
bool comp1(const event &a, const event &b) {
  return a.x < b.x || (a.x == b.x && a.lucky);
}
bool comp2(const event &a, const event &b) {
  return a.x > b.x || (a.x == b.x && !a.lucky);
}
vector<long long> lucky;
long long cur = 0;
void gen_lucky(int len = 1) {
  if (len > 18)
    return;
  else {
    cur = cur * 10ll + 4ll;
    lucky.push_back(cur);
    gen_lucky(len + 1);
    cur /= 10ll;
    cur = cur * 10ll + 7ll;
    lucky.push_back(cur);
    gen_lucky(len + 1);
    cur /= 10ll;
  }
}
vector<seg> a;
vector<event> b, bb;
long long lft[530000], rght[530000];
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].l >> a[i].r, minsize = min(minsize, a[i].r - a[i].l);
  lucky.reserve(530000);
  gen_lucky();
  b.reserve(650000);
  bb.reserve(650000);
  sort(lucky.begin(), lucky.end());
  for (int i = 0; i < lucky.size(); ++i) bb.push_back(event(lucky[i], 1));
  b = bb;
  for (int i = 0; i < n; ++i) b.push_back(event(a[i].r, 0));
  sort(b.begin() + lucky.size(), b.end(), comp1);
  inplace_merge(b.begin(), b.begin() + lucky.size(), b.end(), comp1);
  long long cnt = 0, cost = 0;
  int lcnt = 0;
  if (!b[0].lucky)
    cnt = 1;
  else
    lcnt = 1;
  for (int i = 1; i < b.size(); ++i) {
    cost += cnt * (b[i].x - b[i - 1].x);
    if (cost > k || cost < 0) {
      for (int j = lcnt; j < lucky.size(); ++j) lft[j] = INF;
      break;
    }
    if (b[i].lucky)
      lft[lcnt++] = cost;
    else
      cnt++;
  }
  b = bb;
  for (int i = 0; i < n; ++i) b.push_back(event(a[i].l, 0));
  sort(b.begin() + lucky.size(), b.end(), comp1);
  inplace_merge(b.begin(), b.begin() + lucky.size(), b.end(), comp1);
  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].x - b[i].x);
    if (cost > k || cost < 0) {
      for (int j = lcnt - 1; j >= 0; --j) rght[j] = INF;
      break;
    }
    if (b[i].lucky)
      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;
}
