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