#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, s;
  cin >> n >> s;
  vector<int> times;
  while (n--) {
    int hour, min;
    cin >> hour >> min;
    min += hour * 60;
    times.push_back(min);
  }
  int curr = -s - 1;
  int diff = 2 * s + 1;
  int res = -1;
  for (auto time : times) {
    if (time - curr > diff) {
      res = curr + s + 1;
      break;
    }
    curr = time;
  }
  if (res == -1) {
    res = curr + s + 1;
  }
  cout << res / 60 << " " << res % 60 << endl;
}
