#include <bits/stdc++.h>
using namespace std;
int n, s;
int h, ss, lh, ls, pos;
int main() {
  scanf("%d%d%d%d", &n, &s, &h, &ss);
  int now = h * 60 + ss;
  if (now - 1 >= s) {
    printf("%d %d", 0, 0);
    return 0;
  }
  lh = h, ls = ss;
  for (int i = 2; i <= n; i++) {
    scanf("%d%d", &h, &ss);
    if (h * 60 + ss - now >= 2 * s + 2) {
      printf("%d %d\n", lh + (ls + s + 1) / 60, (ls + s + 1) % 60);
      pos = i;
      break;
    } else
      now = h * 60 + ss, lh = h, ls = ss;
  }
  if (pos != 0)
    for (int i = pos + 1; i <= n; i++) scanf("%d%d", &h, &ss);
  if (pos == 0) {
    printf("%d %d\n", h + (ss + 1 + s) / 60, (ss + 1 + s) % 60);
  }
  return 0;
}
