#include <bits/stdc++.h>
using namespace std;
inline void read() {}
template <typename T, typename... Ts>
inline void read(T& A, Ts&... As) {
  T x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
  A = x * f, read(As...);
}
inline void write(char a) { putchar(a); }
template <typename T>
inline void write(T a) {
  if (a < 0ll) putchar('-'), a = -a;
  if (a > 9ll) write(a / 10ll);
  putchar(a % 10ll + '0');
}
template <typename T, typename... Ts>
inline void write(T A, Ts... As) {
  write(A), write(As...);
}
const long long N = 600010, inf = 1e18, maxn = 30;
long long n, k, p[N], cnt, len = inf;
long long l[N], r[N];
struct bign {
  long long d[maxn], len;
  void clean() {
    while (len > 1 && !d[len - 1]) len--;
  }
  bign() {
    memset(d, 0, sizeof(d));
    len = 1;
  }
  bign(long long num) { *this = num; }
  bign(char* num) { *this = num; }
  bign operator=(const char* num) {
    memset(d, 0, sizeof(d));
    len = strlen(num);
    for (long long i = 0; i < len; i++) d[i] = num[len - 1 - i] - '0';
    clean();
    return *this;
  }
  bign operator=(long long num) {
    char s[maxn];
    sprintf(s, "%lld", num);
    *this = s;
    return *this;
  }
  bign operator+(const bign& b) {
    bign c = *this;
    long long i;
    for (i = 0; i < b.len; i++) {
      c.d[i] += b.d[i];
      if (c.d[i] > 9) c.d[i] %= 10, c.d[i + 1]++;
    }
    while (c.d[i] > 9) c.d[i++] %= 10, c.d[i]++;
    c.len = max(len, b.len);
    if (c.d[i] && c.len <= i) c.len = i + 1;
    return c;
  }
  bign operator-(const bign& b) {
    bign c = *this;
    long long i;
    for (i = 0; i < b.len; i++) {
      c.d[i] -= b.d[i];
      if (c.d[i] < 0) c.d[i] += 10, c.d[i + 1]--;
    }
    while (c.d[i] < 0) c.d[i++] += 10, c.d[i]--;
    c.clean();
    return c;
  }
  bign operator*(const bign& b) const {
    long long i, j;
    bign c;
    c.len = len + b.len;
    for (j = 0; j < b.len; j++)
      for (i = 0; i < len; i++) c.d[i + j] += d[i] * b.d[j];
    for (i = 0; i < c.len - 1; i++) c.d[i + 1] += c.d[i] / 10, c.d[i] %= 10;
    c.clean();
    return c;
  }
  bign operator+=(const bign& b) {
    *this = *this + b;
    return *this;
  }
  bool operator<(const bign& b) const {
    if (len != b.len) return len < b.len;
    for (long long i = len - 1; i >= 0; i--)
      if (d[i] != b.d[i]) return d[i] < b.d[i];
    return false;
  }
  bool operator>(const bign& b) const { return b < *this; }
  bool operator<=(const bign& b) const { return !(b < *this); }
  bool operator>=(const bign& b) const { return !(*this < b); }
  bool operator!=(const bign& b) const { return b < *this || *this < b; }
  bool operator==(const bign& b) const { return !(b < *this) && !(b > *this); }
  string str() const {
    char s[maxn] = {};
    for (long long i = 0; i < len; i++) s[len - 1 - i] = d[i] + '0';
    return s;
  }
};
double suml, sumr;
long long ans;
void dfs(long long x) {
  if (x > inf) return;
  p[++cnt] = x, dfs(x * 10 + 4), dfs(x * 10 + 7);
}
struct cmpl {
  bool operator()(long long x, long long y) {
    return l[x] == l[y] ? x < y : l[x] < l[y];
  }
};
struct cmpr {
  bool operator()(long long x, long long y) {
    return r[x] == r[y] ? x < y : r[x] < r[y];
  }
};
multiset<long long, cmpr> s1;
multiset<long long, cmpl> s2;
bool calc(long long L, long long R) {
  double sz2 = s2.size(), sz1 = double(n) - sz2 - s1.size();
  return (sumr - sz2 * L + sz1 * R - suml) <= double(k);
}
signed main() {
  read(n, k), dfs(4), dfs(7), sort(p + 1, p + cnt + 1);
  for (register long long i = 1; i <= n; ++i)
    read(l[i], r[i]), len = min(len, r[i] - l[i]), s2.insert(i);
  s1.clear(), s2.clear();
  for (register long long i = 1; i <= n; ++i) s2.insert(i), sumr += l[i];
  long long j = 0;
  for (register long long i = 1; i <= cnt; ++i) {
    while (p[j + 1] - p[i] <= len && j < cnt) {
      ++j;
      while (s2.size()) {
        long long k = *s2.begin();
        if (l[k] > p[j]) break;
        s2.erase(k);
        sumr = sumr - l[k];
        s1.insert(k);
      }
      while (s1.size()) {
        long long k = *s1.begin();
        if (r[k] >= p[j]) break;
        s1.erase(k);
        suml = suml + r[k];
      }
      if (calc(p[i], p[j]))
        ans = max(ans, j - i + 1);
      else
        break;
    }
  }
  write(ans);
  return 0;
}
