#include <bits/stdc++.h>
using namespace std;
long long int infinito = 2000000000000000000LL;
long long int safeprod(long long int a, long long int b) {
  long long int c = a * b;
  if (c < 0 or c > infinito or (a != 0 and c / a != b)) return infinito;
  return c;
}
long long int safesum(long long int a, long long int b) {
  long long int c = a + b;
  if (c < 0 or c > infinito) return infinito;
  return c;
}
long long int numlucky;
long long int lucky[1000000];
int main() {
  lucky[0] = 4;
  lucky[1] = 7;
  numlucky = 2;
  int indexini = 0;
  for (int d = 0; d < 17; d++) {
    int len = numlucky;
    for (int i = indexini; i < len; i++) {
      lucky[numlucky++] = lucky[i] * 10 + 4;
      lucky[numlucky++] = lucky[i] * 10 + 7;
    }
    indexini = len;
  }
  sort(lucky, lucky + numlucky);
  long long int n, k;
  cin >> n >> k;
  vector<pair<long long int, long long int> > seg(n);
  for (int i = 0; i < n; i++) cin >> seg[i].first >> seg[i].second;
  vector<long long int> costeder(numlucky);
  vector<long long int> tamanyoder(numlucky);
  {
    vector<pair<long long int, long long int> > orden;
    for (int i = 0; i < numlucky; i++)
      orden.push_back(pair<long long int, long long int>(lucky[i], -i - 1));
    for (int i = 0; i < n; i++)
      orden.push_back(pair<long long int, long long int>(
          seg[i].first, seg[i].second - seg[i].first));
    sort(orden.begin(), orden.end());
    long long int costder = 0;
    long long int tamder = infinito;
    long long int numder = 0;
    long long int lastder = 0;
    for (int i = int(orden.size()) - 1; i >= 0; i--) {
      pair<long long int, long long int> p = orden[i];
      costder = safesum(costder, safeprod(numder, (lastder - p.first)));
      if (p.second < 0) {
        costeder[-1 - p.second] = costder;
        tamanyoder[-1 - p.second] = tamder;
      } else {
        numder++;
        tamder = min(tamder, p.second);
      }
      lastder = p.first;
    }
  }
  vector<long long int> costeizq(numlucky);
  vector<long long int> tamanyoizq(numlucky);
  {
    vector<pair<long long int, long long int> > orden;
    for (int i = 0; i < numlucky; i++)
      orden.push_back(pair<long long int, long long int>(lucky[i], i));
    for (int i = 0; i < n; i++)
      orden.push_back(pair<long long int, long long int>(
          seg[i].second, -1 - (seg[i].second - seg[i].first)));
    sort(orden.begin(), orden.end());
    long long int costizq = 0;
    long long int tamizq = infinito;
    long long int numizq = 0;
    long long int lastizq = 0;
    for (int i = 0; i < int(orden.size()); i++) {
      pair<long long int, long long int> p = orden[i];
      costizq = safesum(costizq, safeprod(numizq, (p.first - lastizq)));
      if (p.second >= 0) {
        costeizq[p.second] = costizq;
        tamanyoizq[p.second] = tamizq;
      } else {
        numizq++;
        tamizq = min(tamizq, -1 - p.second);
      }
      lastizq = p.first;
    }
  }
  int iaux = -1;
  int maximo = 0;
  for (int i = 0; i < numlucky; i++) {
    if (costeder[i] <= k) {
      iaux = max(iaux, i - 1);
      while (iaux + 1 < numlucky and costeder[i] + costeizq[iaux + 1] <= k and
             lucky[iaux + 1] - lucky[i] <= tamanyoder[i] and
             lucky[iaux + 1] - lucky[i] <= tamanyoizq[iaux + 1])
        iaux++;
      if (iaux > i - 1) maximo = max(maximo, iaux - i + 1);
    }
  }
  cout << maximo << endl;
}
