#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void umax(T &a, T b) {
  if (a < b) a = b;
}
template <class T>
inline void umin(T &a, T b) {
  if (a > b) a = b;
}
template <class T>
inline T abs(T a) {
  return a > 0 ? a : -a;
}
template <class T>
inline T gcd(T a, T b) {
  return __gcd(a, b);
}
template <class T>
inline T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}
const int inf = 1e9 + 143;
const long long longinf = 1e18 + 143;
inline int read() {
  int x;
  scanf(" %d", &x);
  return x;
}
const int N = 212121;
int n;
long long k;
long long smallest = longinf;
pair<long long, long long> a[N];
vector<long long> ls, rs;
double suml[N], sumr[N];
vector<long long> nums;
bool bad(long long l, long long r) {
  if (r - l + 1 > smallest) return true;
  int id = lower_bound((rs).begin(), (rs).end(), r) - rs.begin() - 1;
  double res = 0ll;
  if (id >= 0) {
    res += (double)r * (id + 1) - sumr[id];
  }
  id = upper_bound((ls).begin(), (ls).end(), l) - ls.begin();
  if (id < n) {
    double cnt = n - 1 - id + 1;
    res += (suml[n - 1] - (id == 0 ? 0ll : suml[id - 1])) - cnt * l;
  }
  return res > k;
}
int main() {
  n = read();
  scanf(" %I64d", &k);
  for (int i = 1; i <= n; i++) {
    scanf(" %I64d %I64d", &a[i].first, &a[i].second);
    ls.push_back(a[i].first);
    rs.push_back(a[i].second);
    umin(smallest, a[i].second - a[i].first + 1);
  }
  sort((ls).begin(), (ls).end());
  sort((rs).begin(), (rs).end());
  suml[0] = ls[0];
  for (int i = 1; i < n; i++) suml[i] = suml[i - 1] + ls[i];
  sumr[0] = rs[0];
  for (int i = 1; i < n; i++) sumr[i] = sumr[i - 1] + rs[i];
  for (int sz = 1; sz <= 18; sz++) {
    for (int mask = 0; mask < (1 << sz); mask++) {
      long long x = 0;
      for (int i = 0; i < sz; i++) {
        x *= 10ll;
        if ((mask >> i) & 1)
          x += 4ll;
        else
          x += 7ll;
      }
      nums.push_back(x);
    }
  }
  sort((nums).begin(), (nums).end());
  int sz = nums.size();
  int ptr = 0;
  int ans = 0;
  for (int i = 0; i < sz; i++) {
    while (ptr <= i && bad(nums[ptr], nums[i])) ++ptr;
    umax(ans, i - ptr + 1);
  }
  printf("%d\n", ans);
  return 0;
}
