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