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