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