#include <bits/stdc++.h>
using namespace std;
int n, cnt;
long long m, p[1000000], l[111111], r[111111], fl[1000000], fr[1000000], q,
    dmin;
int main() {
  scanf("%d%I64d", &n, &m);
  for (int i = 1; i <= 18; ++i) {
    for (int s = 0; s < (1 << i); ++s) {
      long long k = 0;
      for (int j = (i - 1); j >= (0); --j) k = k * 10 + (s >> j & 1 ? 7 : 4);
      p[cnt++] = k;
    }
  }
  for (int i = 0; i < (n); ++i) {
    scanf("%I64d%I64d", l + i, r + i);
    if (!i || r[i] - l[i] < dmin) dmin = r[i] - l[i];
  }
  sort(l, l + n);
  sort(r, r + n);
  q = 0;
  l[n] = r[n] = 2E18;
  for (int i = 0; i < (cnt); ++i) {
    if (!i)
      fl[i] = 0;
    else {
      if (fl[i - 1] + double(q) * (p[i] - p[i - 1]) > 2E18)
        fl[i] = m + 1;
      else
        fl[i] = fl[i - 1] + q * (p[i] - p[i - 1]);
    }
    if (fl[i] > m) fl[i] = m + 1;
    while (r[q] < p[i]) {
      fl[i] += p[i] - r[q++];
      if (fl[i] > m) fl[i] = m + 1;
    }
  }
  q = n - 1;
  for (int i = (cnt - 1); i >= (0); --i) {
    if (i == cnt - 1)
      fr[i] = 0;
    else {
      if (fr[i + 1] + double(n - 1 - q) * (p[i + 1] - p[i]) > 2E18)
        fr[i] = m + 1;
      else
        fr[i] = fr[i + 1] + (n - 1 - q) * (p[i + 1] - p[i]);
    }
    if (fr[i] > m) fr[i] = m + 1;
    while (l[q] > p[i]) {
      fr[i] += l[q--] - p[i];
      if (fr[i] > m) fr[i] = m + 1;
    }
  }
  int l = 0, r = cnt;
  while (l < r) {
    int md = 1 + l + r >> 1;
    bool ok = false;
    for (int i = (md - 1); i <= (cnt - 1); ++i) {
      if (p[i] - p[i - md + 1] <= dmin && fr[i - md + 1] + fl[i] <= m) {
        ok = true;
        break;
      }
    }
    if (!ok)
      r = md - 1;
    else
      l = md;
  }
  cout << l << endl;
  return 0;
}
