#include <bits/stdc++.h>
#pragma GCC optimize("O3", "strict-aliasing")
using namespace std;
long long l[100005], r[100005];
vector<long long> v;
int sl[1000005];
bool lucky[1000005];
long long mn = 1e18;
long long le[1000005], ri[1000005];
map<long long, int> dp1, dp2;
int main() {
  v.push_back(0);
  for (int i = 1; i <= 18; i++) {
    for (int j = 0; j < (1 << i); j++) {
      long long a = 0;
      for (int k = 0; k < i; k++) {
        if ((j >> k) & 1)
          a = (a * 10) + 4;
        else
          a = (a * 10) + 7;
      }
      v.push_back(a);
    }
  }
  v.push_back(4444444444444444444ll);
  int n;
  long long k;
  scanf("%d%lld", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%lld%lld", &l[i], &r[i]);
    mn = min(mn, r[i] - l[i] + 1);
    v.push_back(l[i]);
    v.push_back(r[i]);
    dp1[l[i]]++;
    dp2[r[i]]++;
  }
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  for (int i = 1; i < v.size(); i++) {
    long long x = v[i];
    lucky[i] = 1;
    while (x) {
      if (x % 10 != 4 && x % 10 != 7) lucky[i] = 0;
      x /= 10;
    }
  }
  int sum = 0;
  for (int i = 1; i < v.size(); i++) {
    long long c = v[i] - v[i - 1];
    le[i] = le[i - 1];
    long double tmp = le[i] + (long double)sum * c;
    if (tmp > 2e18) tmp = 2e18;
    le[i] = (long long)tmp;
    sl[i] += sl[i - 1];
    sl[i] += lucky[i];
    if (dp2.count(v[i])) sum += dp2[v[i]];
  }
  sum = 0;
  for (int i = v.size() - 2; i >= 1; i--) {
    ri[i] = ri[i + 1];
    long long c = v[i + 1] - v[i];
    long double tmp = ri[i] + (long double)sum * c;
    if (tmp > 2e18) tmp = 2e18;
    ri[i] = (long long)tmp;
    if (dp1.count(v[i])) sum += dp1[v[i]];
  }
  int ans = 0;
  for (int i = 1; i < v.size(); i++) {
    long long val1 = v[i];
    int lo = i, hi = v.size() - 1, res = -1;
    while (lo <= hi) {
      int mid = (lo + hi) / 2;
      long long val2 = v[mid];
      if (val2 - val1 + 1 <= mn && le[mid] + ri[i] <= k)
        res = mid, lo = mid + 1;
      else
        hi = mid - 1;
    }
    if (res != -1) ans = max(ans, sl[res] - sl[i - 1]);
  }
  printf("%d\n", ans);
}
