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