#include <bits/stdc++.h>
using namespace std;
long long track[100];
vector<long long> hap;
void sol(int pos) {
  long long ans = 0;
  long long base = 1;
  for (int i = 0; i < pos; i++) {
    ans += track[i] * base;
    base *= 10;
  }
  if (ans) hap.push_back(ans);
  if (pos == 18) return;
  track[pos] = 4;
  sol(pos + 1);
  track[pos] = 7;
  sol(pos + 1);
}
long long n, k;
struct node {
  long long l, r, len;
  node(long long a = 0, long long b = 0) : l(a), r(b) {}
};
struct val {
  long long q, r;
  val(long long a = 0, long long b = 0) : q(a), r(b) {}
};
const long long MAX = 1e5 + 20, MOD = 1e9;
node ns[MAX];
node ns2[MAX];
val zz[MAX], zz2[MAX];
int preY[MAX * 7];
const val operator+(const val &a, const val &b) {
  long long r = a.r + b.r;
  return {a.q + b.q + r / MOD, r % MOD};
}
const val operator*(const val &a, const val &b) {
  long long r = a.r * b.r;
  return {a.q * b.q * MOD + a.q * b.r + b.q * a.r + r / MOD, r % MOD};
}
const val operator-(const val &a, const val &b) {
  long long r = a.r - b.r;
  if (r >= 0)
    return {a.q - b.q, r};
  else {
    assert(r + MOD >= 0 && r + MOD < MOD);
    return {a.q - b.q - 1, r + MOD};
  }
}
bool operator<(const val &a, const val &b) {
  if (a.q != b.q)
    return a.q < b.q;
  else
    return a.r < b.r;
}
bool operator<=(const val &a, const val &b) { return !(b < a); }
void print(const val &v) { printf("v = %lld\n", v.q * MOD + v.r); }
int main() {
  sol(0);
  sort(hap.begin(), hap.end());
  scanf("%lld%lld", &n, &k);
  long long minlen = LLONG_MAX;
  for (int i = 0; i < n; i++) {
    scanf("%lld%lld", &ns[i].l, &ns[i].r);
    ns[i].len = ns[i].r - ns[i].l;
    minlen = min(minlen, ns[i].len);
    ns2[i] = ns[i];
  }
  sort(ns, ns + n, [](const node &a, const node &b) { return a.r < b.r; });
  sort(ns2, ns2 + n, [](const node &a, const node &b) { return a.l < b.l; });
  for (int i = 0; i < n; i++) {
    if (i == 0)
      zz[i] = {ns[i].r / MOD, ns[i].r % MOD};
    else
      zz[i] = zz[i - 1] + val(ns[i].r / MOD, ns[i].r % MOD);
  }
  for (int i = n - 1; i >= 0; i--) {
    if (i == n - 1)
      zz2[i] = {ns2[i].l / MOD, ns2[i].l % MOD};
    else
      zz2[i] = zz2[i + 1] + val(ns2[i].l / MOD, ns2[i].l % MOD);
  }
  for (int i = (int)hap.size() - 1; i >= 0; i--) {
    long long cur = hap[i];
    int l = 0, r = n - 1;
    while (l < r) {
      int mid = (l + r) / 2;
      if (ns2[mid].l >= cur) {
        r = mid;
      } else
        l = mid + 1;
    }
    if (ns2[l].l < cur)
      preY[i] = -1;
    else
      preY[i] = l;
  }
  int ans = 0;
  val limit = val(k / MOD, k % MOD);
  for (int last = 0; last < hap.size(); last++) {
    long long cur = hap[last];
    int l = 0, r = n - 1;
    bool debug = 0;
    while (l < r) {
      int mid = (l + r + 1) / 2;
      if (ns[mid].r <= cur)
        l = mid;
      else
        r = mid - 1;
    }
    val tmp = {0, 0};
    if (ns[l].r <= cur) {
      tmp = val(cur / MOD, cur % MOD) * val(0, l + 1) - zz[l];
    }
    if (debug) print(tmp);
    if (tmp <= limit) {
      val resi = limit - tmp;
      int l = 0, r = last;
      while (l < r) {
        int mid = (l + r) / 2;
        if (hap[last] - hap[mid] <= minlen)
          r = mid;
        else
          l = mid + 1;
      }
      int l1 = l, r1 = last;
      while (l1 < r1) {
        int mid = (l1 + r1) / 2;
        int idx = preY[mid];
        long long cur = hap[mid];
        if (idx == -1 ||
            zz2[idx] - val(0, n - idx) * val(cur / MOD, cur % MOD) <= resi)
          r1 = mid;
        else
          l1 = mid + 1;
      }
      int idx = preY[l1];
      long long cur = hap[l1];
      if (idx == -1 ||
          zz2[idx] - val(0, n - idx) * val(cur / MOD, cur % MOD) <= resi)
        ans = max(last - l1 + 1, ans);
    }
  }
  printf("%d\n", ans);
  return 0;
}
