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