#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, s;
  int h, m;
  int num, ans = 0;
  int current = 0;
  int flag = 1;
  cin >> n >> s;
  for (int i = 0; i < n; i++) {
    cin >> h >> m;
    num = h * 60 + m;
    if (i == 0) {
      if (num >= s + 1) {
        ans = 0;
        flag = 0;
      }
    }
    if (num - current >= s * 2 + 2 && flag) {
      ans = current + s + 1;
      flag = 0;
    } else {
      current = num;
    }
  }
  if (flag) {
    ans = current + s + 1;
  }
  cout << ans / 60 << " " << ans % 60 << endl;
  return 0;
}
