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