#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, t;
  cin >> n >> t;
  int hr = 0;
  int fh, fm, lf, lm;
  hr = 0;
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    int a, b, tm;
    cin >> a >> b;
    if (i == 1) {
      fh = a;
      fm = b;
    }
    if (i == n) {
      lf = a;
      lm = b;
    }
    if (i == 1 || i == n) tm = 1;
    if (((((a * 60) + b) - hr)) > (2 * t) + 1) {
      ans = hr + t + 1;
    } else
      hr = (a * 60) + b;
  }
  if (((fh * 60) + fm) >= t + 1)
    cout << 0 << " " << 0 << endl;
  else if (ans)
    cout << ans / 60 << " " << ans - ((ans / 60) * 60);
  else
    cout << ((lf * 60) + lm + t + 1) / 60 << " "
         << ((lf * 60) + lm + t + 1) - ((((lf * 60) + lm + t + 1) / 60) * (60));
}
