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