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