#include <bits/stdc++.h>
using namespace std;
int n;
long long k;
struct segment {
  long long b, e;
};
segment s[100010];
struct evnt {
  int segId;
  long long pos;
  char type;
  bool operator<(const evnt &ot) const { return pos < ot.pos; }
};
evnt e[200010];
vector<long long> happy;
int count(long long number) {
  ++number;
  vector<long long>::iterator it =
      lower_bound(happy.begin(), happy.end(), number);
  return it - happy.begin();
}
long long L[1100000];
long long R[1100000];
int main() {
  happy.push_back(4);
  happy.push_back(7);
  const long long lim = 100000000000000000ll;
  for (int i = 0; happy[i] <= lim; i++) {
    happy.push_back(happy[i] * 10 + 4);
    happy.push_back(happy[i] * 10 + 7);
  }
  sort(happy.begin(), happy.end());
  cin >> n >> k;
  const long long INF = 0x7f7f7f7f7f7f7f7f;
  long long minLen = INF;
  for (int i = 0; i < n; i++) {
    cin >> s[i].b >> s[i].e;
    minLen = min(minLen, s[i].e - s[i].b);
    e[i << 1].segId = i;
    e[i << 1].pos = s[i].b;
    e[i << 1].type = 'b';
    e[(i << 1) + 1].segId = i;
    e[(i << 1) + 1].pos = s[i].e;
    e[(i << 1) + 1].type = 'e';
  }
  sort(e, e + n * 2);
  long long cost = 0;
  int movedLeft = 0;
  long long prv = 1000000000000000000ll;
  int pos = n * 2 - 1;
  for (int i = happy.size() - 1; i >= 0; i--) {
    while (pos >= 0 && e[pos].pos >= happy[i]) {
      if (e[pos].type == 'e') {
        --pos;
        continue;
      }
      if ((double)(prv - e[pos].pos) * movedLeft + cost > 2e18)
        cost = INF;
      else
        cost += (prv - e[pos].pos) * movedLeft;
      prv = e[pos].pos;
      ++movedLeft;
      --pos;
    }
    if ((double)(prv - happy[i]) * movedLeft + cost > 2e18)
      cost = INF;
    else
      cost += (prv - happy[i]) * movedLeft;
    prv = happy[i];
    L[i] = cost;
  }
  long long movedRight = 0;
  cost = 0;
  pos = 0;
  prv = 0;
  for (int i = 0; i < happy.size(); i++) {
    while (pos < n * 2 && e[pos].pos <= happy[i]) {
      if (e[pos].type == 'b') {
        ++pos;
        continue;
      }
      if ((double)(e[pos].pos - prv) * movedRight + cost > 2e18)
        cost = INF;
      else
        cost += (e[pos].pos - prv) * movedRight;
      prv = e[pos].pos;
      ++movedRight;
      ++pos;
    }
    if ((double)(happy[i] - prv) * movedRight + cost > 2e18)
      cost = INF;
    else
      cost += (happy[i] - prv) * movedRight;
    prv = happy[i];
    R[i] = cost;
  }
  int best = 0;
  int finish = 0;
  for (int start = 0; start < happy.size(); ++start) {
    long long minCost = L[start];
    finish = max(finish, start);
    if (minCost > k) continue;
    if (R[finish] > k) break;
    if (minCost + R[finish] > k) continue;
    while (finish < happy.size() && happy[finish] - happy[start] <= minLen &&
           minCost + R[finish] <= k)
      ++finish;
    --finish;
    best = max(best, finish - start + 1);
  }
  cout << best << endl;
  return 0;
}
