#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, s;
  scanf("%d%d", &n, &s);
  int ah = -1, am, p;
  for (int i = 0; i < n; ++i) {
    int h, m;
    scanf("%d%d", &h, &m);
    bool f = false;
    if (i == 0) {
      int x = h * 60 + m;
      if (x >= 1 + s) {
        ah = 0, am = 0;
        break;
      }
    } else {
      int x = h * 60 + m;
      if (x - p >= s * 2 + 2) {
        ah = (p + s + 1) / 60;
        am = (p + s + 1) % 60;
        break;
      }
    }
    p = h * 60 + m;
  }
  if (ah == -1) {
    ah = (p + 1 + s) / 60;
    am = (p + 1 + s) % 60;
  }
  printf("%d %d\n", ah, am);
}
