#include <bits/stdc++.h>
namespace wtf {
const long long INF = 1e18 + 233333;
const int N = 101000, M = 1000000;
long long plus(long long a, long long b) { return a > INF - b ? INF : a + b; }
long long multiply(long long a, long long b) {
  return b == 0 ? 0 : (a > INF / b ? INF : a * b);
}
struct node {
  int ty;
  long long pos;
  int id;
  node(int a = 0, long long b = 0, int c = 0) : ty(a), pos(b), id(c) {}
  bool operator<(const node &A) const { return pos < A.pos; }
};
std::vector<node> V;
long long seq[M], f[M], g[M];
int tot;
void predfs(long long x) {
  if (x > INF * 2) return;
  seq[++tot] = x;
  predfs(x * 10 + 4), predfs(x * 10 + 7);
}
int n;
long long k, lim;
void initialize() {
  predfs(4), predfs(7);
  std::sort(seq + 1, seq + tot + 1);
  for (int i = 1; i <= tot; i++) V.push_back(node(0, seq[i], i));
  scanf("%d%I64d", &n, &k);
  long long l, r;
  lim = INF;
  for (int i = 1; i <= n; i++) {
    scanf("%I64d%I64d", &l, &r);
    lim = std::min(lim, r - l + 1);
    V.push_back(node(1, l));
    V.push_back(node(2, r));
  }
  std::sort(V.begin(), V.end());
  long long sum = 0, siz = 0, lst = 0;
  for (int i = 0; i < (int)V.size(); i++)
    if (V[i].ty == 2) {
      sum = plus(sum, multiply(V[i].pos - lst, siz));
      lst = V[i].pos, siz++;
    } else if (V[i].ty == 0) {
      sum = plus(sum, multiply(V[i].pos - lst, siz));
      lst = V[i].pos, f[V[i].id] = sum;
    }
  sum = 0, siz = 0, lst = INF;
  for (int i = V.size() - 1; ~i; i--)
    if (V[i].ty == 1) {
      sum = plus(sum, multiply(lst - V[i].pos, siz));
      lst = V[i].pos, siz++;
    } else if (V[i].ty == 0) {
      sum = plus(sum, multiply(lst - V[i].pos, siz));
      lst = V[i].pos, g[V[i].id] = sum;
    }
}
int getans() {
  int ret = 0, L, R;
  for (L = R = 1; L <= tot; L++) {
    R = std::max(L, R);
    if (f[L] + g[R] > k) continue;
    while (R < tot && seq[R + 1] - seq[L] + 1 <= lim && f[L] + g[R + 1] <= k)
      R++;
    ret = std::max(ret, R - L + 1);
  }
  return ret;
}
void solve() {
  initialize();
  printf("%d\n", getans());
}
}  // namespace wtf
int main() {
  wtf::solve();
  return 0;
}
