#include <bits/stdc++.h>
int main() {
  int n1, n2, n3, n4, n5 = 0;
  std::cin >> n1 >> n2;
  n5 = -(n2 + 1);
  for (int i = 0; i < n1; i++) {
    std::cin >> n3 >> n4;
    if (n3 * 60 + n4 >= n5 + 2 * n2 + 2)
      return std::cout << (n5 + n2 + 1) / 60 << " " << (n5 + n2 + 1) % 60, 0;
    else
      n5 = n3 * 60 + n4;
  }
  std::cout << (n5 + n2 + 1) / 60 << " " << (n5 + n2 + 1) % 60;
}
