#include <bits/stdc++.h>
using namespace std;
inline long long getint() {
  long long _x = 0, _tmp = 1;
  char _tc = getchar();
  while ((_tc < '0' || _tc > '9') && _tc != '-') _tc = getchar();
  if (_tc == '-') _tc = getchar(), _tmp = -1;
  while (_tc >= '0' && _tc <= '9') _x *= 10, _x += (_tc - '0'), _tc = getchar();
  return _x * _tmp;
}
inline long long add(long long _x, long long _y,
                     long long _mod = 1000000007ll) {
  long long _ = _x + _y;
  if (_ >= _mod) _ -= _mod;
  return _;
}
inline long long sub(long long _x, long long _y,
                     long long _mod = 1000000007ll) {
  long long _ = _x - _y;
  if (_ < 0) _ += _mod;
  return _;
}
inline long long mul(long long _x, long long _y,
                     long long _mod = 1000000007ll) {
  long long _ = _x * _y;
  if (_ >= _mod) _ %= _mod;
  return _;
}
long long mypow(long long _a, long long _x, long long _mod) {
  if (_x == 0) return 1ll;
  long long _tmp = mypow(_a, _x / 2, _mod);
  _tmp = mul(_tmp, _tmp, _mod);
  if (_x & 1) _tmp = mul(_tmp, _a, _mod);
  return _tmp;
}
long long mymul(long long _a, long long _x, long long _mod) {
  if (_x == 0) return 0ll;
  long long _tmp = mymul(_a, _x / 2, _mod);
  _tmp = add(_tmp, _tmp, _mod);
  if (_x & 1) _tmp = add(_tmp, _a, _mod);
  return _tmp;
}
inline bool equal(double _x, double _y) {
  return _x > _y - 1e-9 && _x < _y + 1e-9;
}
int __ = 1, _cs;
vector<long long> lucky;
void go(long long now) {
  if (now) lucky.push_back(now);
  if (now > 1000000000000000000ll) return;
  go(now * 10 + 4);
  go(now * 10 + 7);
}
void build() {
  go(0);
  sort((lucky).begin(), (lucky).end());
}
long long n, k, l[101010], r[101010], mn;
void init() {
  n = getint();
  k = getint();
  mn = 1000000000000000000ll;
  for (int i = 0; i < n; i++) {
    l[i] = getint();
    r[i] = getint();
    mn = min(mn, r[i] - l[i] + 1);
  }
  sort(l, l + n);
  sort(r, r + n);
}
long double suml, sumr;
int lptr, rptr;
inline bool okay(long long lv, long long rv) {
  if (rv - lv + 1 > mn) return false;
  while (lptr < n && l[lptr] <= lv) suml -= l[lptr++];
  while (rptr < n && r[rptr] < rv) sumr += r[rptr++];
  long double cstl = suml - (long double)(n - lptr) * (long double)lv;
  long double cstr = (long double)rptr * (long double)rv - sumr;
  return cstl + cstr <= k;
}
void solve() {
  int ans = 0;
  size_t j = 0;
  for (int i = 0; i < n; i++) suml += l[i];
  for (size_t i = 0; i < lucky.size(); i++) {
    j = max(j, i);
    while (j < lucky.size() && okay(lucky[i], lucky[j])) j++;
    ans = max(ans, (int)(j - i));
  }
  cout << ans << endl;
}
int main() {
  build();
  while (__--) {
    init();
    solve();
  }
}
