#include <bits/stdc++.h>
using namespace std;
const long long int INF = (long long int)2e18;
void GenerateAllLuckies(vector<long long int>& luckies) {
  for (int i = 1; i <= 18; i++) {
    for (long long int j = 0; j < (1LL << i); j++) {
      long long int t = 0;
      for (int z = 0; z < i; z++) {
        if (j & (1 << z))
          t = t * 10 + 4;
        else
          t = t * 10 + 7;
      }
      luckies.push_back(t);
    }
  }
}
bool CmpPairSecond(const pair<long long int, long long int>& a,
                   const pair<long long int, long long int>& b) {
  return a.second < b.second;
}
struct point {
  long long int x;
  bool is_lucky;
  int index;
};
bool cmp_point(const point& a, const point& b) { return a.x < b.x; }
long long int mul(long long int a, long long int b) {
  if (b == 0) return 0;
  if (a == 0) return 0;
  if (a > ((INF / b) + 1)) return INF;
  if (a * b > INF) return INF;
  return a * b;
}
void FillSp(const vector<long long int>& luckies,
            const vector<pair<long long int, long long int> >& r, bool is_right,
            vector<point>& sp) {
  sp.clear();
  for (int i = 0; i < luckies.size(); i++) {
    point p;
    p.x = luckies[i];
    p.is_lucky = true;
    p.index = i;
    sp.push_back(p);
  }
  for (int i = 0; i < r.size(); i++) {
    point p;
    if (is_right)
      p.x = r[i].second;
    else
      p.x = r[i].first;
    p.is_lucky = false;
    p.index = -1;
    sp.push_back(p);
  }
}
int main() {
  int n;
  long long int k;
  vector<pair<long long int, long long int> > r;
  long long int min_size = INF;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    pair<long long int, long long int> t;
    cin >> t.first >> t.second;
    if ((t.second - t.first) < min_size) {
      min_size = t.second - t.first;
    }
    r.push_back(t);
  }
  vector<long long int> luckies;
  GenerateAllLuckies(luckies);
  sort(luckies.begin(), luckies.end());
  vector<long long int> left(luckies.size()), right(luckies.size());
  vector<point> sp;
  FillSp(luckies, r, true, sp);
  sort(sp.begin(), sp.end(), cmp_point);
  long long int sum = 0;
  int count = 0;
  long long int prev = 0;
  for (int i = 0; i < sp.size(); i++) {
    if (sum != INF) {
      long long int temp = mul(count, sp[i].x - prev);
      sum = sum + temp;
      if (sum > INF) sum = INF;
      prev = sp[i].x;
    }
    if (!sp[i].is_lucky) count++;
    if (sp[i].is_lucky) {
      left[sp[i].index] = sum;
    }
  }
  FillSp(luckies, r, false, sp);
  sort(sp.begin(), sp.end(), cmp_point);
  sum = 0;
  count = 0;
  prev = 0;
  for (int i = sp.size() - 1; i >= 0; i--) {
    if (sum != INF) {
      long long int temp = mul(count, prev - sp[i].x);
      sum = sum + temp;
      if (sum > INF) sum = INF;
      prev = sp[i].x;
    }
    if (!sp[i].is_lucky) {
      count++;
    }
    if (sp[i].is_lucky) {
      right[sp[i].index] = sum;
    }
  }
  int yk1 = 0;
  int yk2 = 0;
  int ans = 0;
  if (left[yk1] + right[yk1] < k) {
    ans = 1;
  }
  while (true) {
    while (yk2 < luckies.size()) {
      if (left[yk2] + right[yk1] <= k) {
        if (((yk2 - yk1 + 1) > ans) &&
            ((luckies[yk2] - luckies[yk1]) <= min_size)) {
          ans = yk2 - yk1 + 1;
        }
      }
      if (left[yk2] + right[yk1] > k) break;
      if ((luckies[yk2] - luckies[yk1]) > min_size) break;
      yk2++;
    }
    yk1++;
    if (yk2 < yk1) yk2++;
    if (yk2 == luckies.size()) break;
  }
  cout << ans << endl;
  return 0;
}
