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