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