#include <bits/stdc++.h>
using namespace std;
template <class T>
T abs(T x) {
  return x > 0 ? x : (-x);
}
template <class T>
T sqr(T x) {
  return x * x;
}
const long long inf = (long long)(2e18);
vector<long long> gen() {
  vector<long long> res;
  for (int len = 1; len <= 18; ++len)
    for (int mask = 0; mask < (1 << len); ++mask) {
      long long cur = 0;
      for (int i = 0; i < len; ++i)
        cur = cur * 10LL + (long long)(((mask >> i) & 1) ? 7 : 4);
      if (cur < 0) cerr << "AAAA\n";
      res.push_back(cur);
    }
  sort((res).begin(), (res).end());
  return res;
}
vector<long long> l, r;
vector<long long> sl, sr;
int n;
long long mult(long long x, long long y) {
  if (y == 0) return 0;
  if (x == 0) return 0;
  if (x > inf / y + 2LL) return inf;
  long long res = x * y;
  if (res > inf) res = inf;
  return res;
}
long long _getL(int ind, long long x) {
  long long res = 0;
  for (int i = ind; i < ((int)(l).size()); ++i) {
    res += l[i] - x;
    res = min(res, inf);
  }
  return res;
}
long long _getR(int ind, long long x) {
  long long res = 0;
  for (int i = 0; i <= ind; ++i) {
    res += x - r[i];
    res = min(res, inf);
  }
  return res;
}
long long getL(int ind, long long x) {
  long long res = sl[ind];
  long long val = ind >= ((int)(l).size()) ? 0LL : (l[ind] - x);
  res += mult(val, ((int)(l).size()) - ind);
  if (res > inf) res = inf;
  return res;
}
long long getR(int ind, long long x) {
  long long res = sr[ind + 1];
  long long val = ind == -1 ? 0LL : (x - r[ind]);
  res += mult(val, ind + 1);
  if (res > inf) res = inf;
  return res;
}
long long calc(long long x, long long y) {
  int il = lower_bound((l).begin(), (l).end(), x) - l.begin();
  int ir = lower_bound((r).begin(), (r).end(), y) - r.begin();
  if (ir == ((int)(r).size())) --ir;
  while (ir >= 0 && ir < ((int)(r).size()) && r[ir] > y) --ir;
  long long res = getL(il, x);
  res += getR(ir, x);
  return min(res, inf);
}
int main() {
  cin >> n;
  l.resize(n);
  r.resize(n);
  long long k;
  cin >> k;
  long long M = inf;
  for (int i = 0; i < n; ++i) {
    cin >> l[i] >> r[i];
    M = min(M, r[i] - l[i]);
  }
  sort((l).begin(), (l).end());
  sort((r).begin(), (r).end());
  sl.resize(((int)(l).size()) + 1);
  sl[((int)(sl).size()) - 1] = 0;
  for (int i = ((int)(l).size()) - 1; i >= 0; --i) {
    sl[i] = sl[i + 1];
    long long d = i == ((int)(l).size()) - 1 ? 0LL : (l[i + 1] - l[i]);
    d = mult(d, ((int)(l).size()) - i - 1);
    sl[i] += d;
    if (sl[i] > inf) sl[i] = inf;
  }
  sr.resize(((int)(r).size()) + 1);
  sr[0] = 0;
  for (int i = 0; i < ((int)(r).size()); ++i) {
    sr[i + 1] = sr[i];
    long long d = i == 0 ? 0LL : (r[i] - r[i - 1]);
    d = mult(d, i);
    sr[i + 1] += d;
    if (sr[i + 1] > inf) sr[i + 1] = inf;
  }
  long long res = 0;
  vector<long long> v = gen();
  for (int i = 0; i < ((int)(v).size()); ++i) {
    int down = i, up = ((int)(v).size());
    while (up - down > 1) {
      int t = (up + down) / 2;
      if (v[t] - v[i] > M) {
        up = t;
        continue;
      }
      if (calc(v[i], v[t]) > k)
        up = t;
      else
        down = t;
    }
    if (calc(v[i], v[i]) > k) up = down;
    res = max(res, (long long)(up - i));
  }
  cout << res << "\n";
  return 0;
}
