#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, s;
  cin >> n >> s;
  vector<int> minutes;
  for (int i = 0; i < n; ++i) {
    int h, m;
    cin >> h >> m;
    minutes.push_back(60 * h + m);
  }
  int tv;
  if (minutes[0] >= s + 1) {
    tv = 0;
    cout << tv / 60 << ' ' << tv % 60 << endl;
    exit(0);
  }
  for (int i = 0; i < n - 1; ++i) {
    if (minutes[i + 1] - minutes[i] >= 2 * s + 2) {
      tv = minutes[i] + s + 1;
      cout << tv / 60 << ' ' << tv % 60 << endl;
      exit(0);
    }
  }
  tv = minutes.back() + s + 1;
  cout << tv / 60 << ' ' << tv % 60 << endl;
  return 0;
}
