#include <bits/stdc++.h>
using namespace std;
void fast_in_out() {
  std::ios_base::sync_with_stdio(NULL);
  cin.tie(NULL);
  cout.tie(NULL);
}
void pause() {}
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, -1, 0, 1};
int arr[200];
int main() {
  fast_in_out();
  int n, s;
  cin >> n >> s;
  for (int i = 0; i < n; i++) {
    int h, m;
    cin >> h >> m;
    arr[i] = h * 60 + m;
  }
  int ans = arr[n - 1] + s + 1;
  for (int i = 0; i < n - 1; i++) {
    int ansy = arr[i] + 1 + s;
    if (ansy + s < arr[i + 1]) {
      ans = ansy;
      break;
    }
  }
  if (s < arr[0]) ans = 0;
  cout << ans / 60 << " " << ans % 60 << '\n';
  pause();
  return 0;
}
