#include <bits/stdc++.h>
using namespace std;
int space(int a, int b, int c, int d) { return c * 60 + d - a * 60 - b; }
int main() {
  int n, s, i;
  cin >> n >> s;
  int h[n], m[n];
  for (i = 0; i < n; i++) cin >> h[i] >> m[i];
  int test = 0;
  if (space(0, 0, h[0], m[0]) >= s + 1)
    cout << 0 << " " << 0;
  else {
    for (i = 0; i < n - 1; i++) {
      if (space(h[i], m[i], h[i + 1], m[i + 1]) >= 2 * s + 2) {
        test = 1;
        int temp = h[i] * 60 + m[i] + s + 1;
        cout << floor(temp / 60) << " " << temp % 60;
        break;
      }
    }
    if (test == 0) {
      int temp = h[n - 1] * 60 + m[n - 1] + s + 1;
      cout << floor(temp / 60) << " " << temp % 60;
    }
  }
}
