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