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