#include <bits/stdc++.h>
using namespace std;
int h[105], m[105];
int a[105];
int t, n, s;
int main() {
  scanf("%d%d", &n, &s);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &h[i], &m[i]);
  }
  for (int i = n; i > 0; --i) {
    a[i] = (h[i] - h[i - 1]) * 60 + m[i] - m[i - 1];
  }
  int fg = 1;
  if (a[1] > s) {
    printf("%d %d\n", 0, 0);
    fg = 0;
  } else {
    for (int i = 1; i <= n; ++i) {
      if (a[i] >= s * 2 + 2) {
        fg = 0;
        printf("%d %d\n", h[i - 1] + (m[i - 1] + s + 1) / 60,
               (m[i - 1] + s + 1) % 60);
        break;
      }
    }
  }
  if (fg) {
    printf("%d %d\n", h[n] + (m[n] + 1 + s) / 60, (m[n] + s + 1) % 60);
  }
  return 0;
}
