#include <bits/stdc++.h>
using namespace std;
const long long inf = 1000000007LL;
const double EPS = 1e-10;
const long long inf_ll = (long long)1e18;
const long long maxn = 105LL;
const long long mod = 810872LL;
int n, S;
int a[maxn], b[maxn];
inline int dist(int h, int m) { return h * 60 + m; }
int main() {
  scanf("%d%d", &n, &S);
  for (register int(i) = (1); (i) <= (n); ++i) scanf("%d%d", a + i, b + i);
  if (dist(a[1], b[1]) > S) {
    puts("0 0");
    return 0;
  }
  for (register int(i) = (2); (i) <= (n); ++i)
    if (dist(a[i] - a[i - 1], b[i] - b[i - 1]) >= S * 2 + 2) {
      int tmp = b[i - 1] + S + 1;
      printf("%d %d\n", a[i - 1] + tmp / 60, tmp % 60);
      return 0;
    }
  int tmp = b[n] + S + 1;
  printf("%d %d\n", a[n] + tmp / 60, tmp % 60);
  return 0;
}
