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