#include <bits/stdc++.h>
using namespace std;
int vn;
vector<long long> v;
void dfs(int k, long long x) {
  v.push_back(x);
  if (k == 18) return;
  dfs(k + 1, x * 10 + 4);
  dfs(k + 1, x * 10 + 7);
}
void init() {
  dfs(0, 0);
  sort(v.begin(), v.end());
  vn = v.size();
  v.push_back(1LL << 61);
}
int n;
long long m, len;
long long a[100005], b[100005];
long long fl[(1 << 19)], fr[(1 << 19)];
inline void add(long long& ret, long long delta) {
  ret = min(ret + delta, m + 1);
}
inline long long mul(long long a, long long b) {
  if (!a || !b) return 0;
  return m / a < b ? m + 1 : a * b;
}
void read() {
  scanf("%d %I64d", &n, &m);
  len = 1LL << 60;
  for (int i = 0; i < n; ++i) {
    scanf("%I64d %I64d", &a[i], &b[i]);
    len = min(len, b[i] - a[i]);
  }
  sort(a, a + n);
  sort(b, b + n);
  long long sum = 0;
  int k = 0;
  for (int i = 1; i < vn; ++i) {
    add(sum, mul(k, v[i] - v[i - 1]));
    while (k < n && b[k] <= v[i]) {
      add(sum, v[i] - b[k]);
      ++k;
    }
    fl[i] = sum;
  }
  sum = 0;
  k = n - 1;
  for (int i = vn - 1; i >= 1; --i) {
    add(sum, mul(n - 1 - k, v[i + 1] - v[i]));
    while (k >= 0 && v[i] <= a[k]) {
      add(sum, a[k] - v[i]);
      --k;
    }
    fr[i] = sum;
  }
}
int solve() {
  int l = 1, r = vn - 1;
  while (l <= r) {
    int mid = (l + r) >> 1;
    bool flag = 0;
    for (int i = 1; i + mid - 1 < vn && !flag; ++i)
      if (v[i + mid - 1] - v[i] <= len && fr[i] + fl[i + mid - 1] <= m)
        flag = 1;
    if (flag)
      l = mid + 1;
    else
      r = mid - 1;
  }
  return l - 1;
}
int main() {
  init();
  read();
  printf("%d\n", solve());
  return 0;
}
