#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  int N, S;
  cin >> N >> S;
  vector<int> A;
  for (int i = 0; i < N; i++) {
    int c, b;
    cin >> c >> b;
    A.push_back(c * 60 + b);
  }
  if (A[0] >= (S + 1)) {
    cout << "0 0\n";
    return 0;
  }
  for (int i = 1; i < N; i++) {
    if ((A[i] - A[i - 1]) >= (2 * S + 2)) {
      int out = A[i - 1] + S + 1;
      cout << out / 60 << " " << out % 60 << '\n';
      return 0;
    }
  }
  cout << (A[(int)A.size() - 1] + S + 1) / 60 << " "
       << (A[(int)A.size() - 1] + S + 1) % 60 << '\n';
}
