#include <bits/stdc++.h>
using namespace std;
const int MaxN = 100011;
const int MaxT = (1 << 19) + 111;
struct node {
  long long l, r;
};
bool cmp1(node a, node b) { return a.r < b.r; }
bool cmp2(node a, node b) { return a.l < b.l; }
node s[MaxN];
long long nums[MaxT];
long long minsize;
long long _left[MaxT], _right[MaxT];
int t = 0;
void dfs(int x, long long y) {
  if (y) nums[++t] = y;
  if (x == 18) return;
  dfs(x + 1, y * 10 + 4);
  dfs(x + 1, y * 10 + 7);
}
int main() {
  int n;
  long long k;
  scanf("%d%I64d", &n, &k);
  dfs(0, 0);
  sort(nums + 1, nums + t + 1);
  minsize = 1000000000000000000LL;
  for (int i = 0; i < n; i++) {
    scanf("%I64d%I64d", &s[i].l, &s[i].r);
    minsize = min(minsize, s[i].r - s[i].l);
  }
  sort(s, s + n, cmp1);
  for (int i = 1, j = 0; i <= t; i++) {
    if ((k - _right[i - 1]) / (nums[i] - nums[i - 1]) + 1 < j)
      _right[i] = k + 1;
    else
      _right[i] = min(k + 1LL, _right[i - 1] + j * (nums[i] - nums[i - 1]));
    for (; j != n && s[j].r <= nums[i]; j++)
      _right[i] = min(k + 1LL, _right[i] + nums[i] - s[j].r);
  }
  sort(s, s + n, cmp2);
  for (int i = t, j = n - 1; i; i--) {
    if ((k - _left[i + 1]) / (nums[i + 1] - nums[i]) + 1 < n - 1 - j)
      _left[i] = k + 1;
    else
      _left[i] =
          min(k + 1LL, _left[i + 1] + (n - 1 - j) * (nums[i + 1] - nums[i]));
    for (; j != -1 && s[j].l >= nums[i]; j--)
      _left[i] = min(k + 1LL, _left[i] + s[j].l - nums[i]);
  }
  int ans = 0;
  for (int i = 1, j = 1; i <= t; i++) {
    while (j <= i && (_right[i] + _left[j] > k || nums[i] - nums[j] > minsize))
      j++;
    if (j <= i) ans = max(ans, i - j + 1);
  }
  printf("%d\n", ans);
  return 0;
}
