#include <bits/stdc++.h>
#pragma GCC optimize(3)
using namespace std;
int n, s, x[200], y[200];
signed main() {
  scanf("%d%d", &n, &s);
  for (int i = 2; i <= n + 1; i++) {
    scanf("%d%d", x + i, y + i);
  }
  if (x[2] >= 0 && y[2] >= 0) {
    int now = x[2] * 60 + y[2];
    if (now >= s + 1) return 0 * printf("0 0\n");
  }
  x[n + 2] = y[n + 2] = 0x3f3f3f3f;
  for (int i = 3; i <= n + 2; i++) {
    int a = x[i - 1] * 60 + y[i - 1];
    int b = x[i] * 60 + y[i];
    if (b - a >= 2 * s + 2) {
      int c = x[i - 1], d = y[i - 1];
      d += s + 1;
      c += d / 60;
      d %= 60;
      return 0 * printf("%d %d\n", c, d);
    }
  }
}
