#include <bits/stdc++.h>
using namespace std;
const long long MAX_N = 1e5 + 5, inf = 1000000LL * 1000000LL * 1000000LL;
long long n, k, ans, l = inf;
long long L[MAX_N];
long long psumL[MAX_N];
long long R[MAX_N];
long long psumR[MAX_N];
vector<long long> lucky;
void preProcess() {
  lucky.resize(1 << 19);
  long long ind = 0;
  for (long long len = 1; len <= 18; len++)
    for (long long mask = 0; mask < (1 << len); mask++) {
      long long val = 0;
      for (long long i = 0; i < len; i++)
        val = val * 10 + ((mask >> i) & 1 ? 7 : 4);
      lucky[ind++] = val;
    }
  lucky.resize(ind);
  sort(lucky.begin(), lucky.end());
}
bool can(long long i, long long j) {
  long long luck1 = lucky[i], luck2 = lucky[j], val = 0;
  if (l < luck2 - luck1 + 1) return false;
  long long indL = lower_bound(L, L + n, luck1) - L;
  long long indR = lower_bound(R, R + n, luck2) - R - 1;
  if (k < psumL[indL]) return false;
  if (log(L[indL] - luck1) + log(n - indL) > log(k - psumL[indL])) return false;
  val += psumL[indL] + (L[indL] - luck1) * (n - indL);
  if (k < val) return false;
  if (indR != -1) {
    if (k - val < psumR[indR]) return false;
    if (log(luck2 - R[indR]) + log(indR + 1) > log(k - val - psumR[indR]))
      return false;
    val += psumR[indR] + (luck2 - R[indR]) * (indR + 1);
  }
  return val <= k;
}
int main() {
  preProcess();
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n >> k;
  for (long long i = 0; i < n; i++) {
    cin >> L[i] >> R[i];
    l = min(R[i] - L[i] + 1, l);
  }
  sort(L, L + n);
  sort(R, R + n);
  for (long long i = n - 2; ~i; i--) {
    if (psumL[i + 1] == inf) {
      psumL[i] = inf;
      continue;
    }
    if (log(L[i + 1] - L[i]) + log(n - 1 - i) > log(inf - psumL[i + 1])) {
      psumL[i] = inf;
      continue;
    }
    psumL[i] = psumL[i + 1] + (L[i + 1] - L[i]) * (n - 1 - i);
  }
  for (long long i = 1; i < n; i++) {
    if (psumR[i - 1] == inf) {
      psumR[i] = inf;
      continue;
    }
    if (log(R[i] - R[i - 1]) + log(i) > log(inf - psumR[i - 1])) {
      psumL[i] = inf;
      continue;
    }
    psumR[i] = psumR[i - 1] + (R[i] - R[i - 1]) * i;
  }
  for (long long i = 0; i < lucky.size(); i++) {
    if (!can(i, i)) continue;
    long long s = i, e = lucky.size();
    while (e - s > 1) {
      long long med = e + s >> 1;
      if (can(i, med))
        s = med;
      else
        e = med;
    }
    ans = max(ans, s - i + 1);
  }
  cout << ans << "\n";
  return 0;
}
