#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, s;
  cin >> n >> s;
  long long h, m;
  cin >> h >> m;
  long long last = h * 60 + m;
  if (s < last) {
    cout << "0 0" << endl;
    return 0;
  }
  for (int i = 1; i < n; i++) {
    cin >> h >> m;
    long long curr = h * 60 + m;
    if (curr - last > (s * 2) + 1) {
      break;
    }
    last = curr;
  }
  last += s + 1;
  long long lastH = last / 60, lastM = last % 60;
  cout << lastH << " " << lastM << endl;
  return 0;
}
