#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, s;
  cin >> n >> s;
  vector<long long> a(n);
  for (int i = 0; i < n; i++) {
    long long h, m;
    scanf("%lld%lld", &h, &m);
    a[i] = h * 60 + m;
  }
  sort(a.begin(), a.end());
  if (a[0] >= s + 1) {
    cout << 0 << " " << 0 << endl;
    return 0;
  }
  for (int i = 1; i < n; i++) {
    if (a[i] - a[i - 1] >= 2 * (s + 1)) {
      long long res = a[i - 1] + s + 1;
      cout << res / 60 << " " << res % 60 << endl;
      return 0;
    }
  }
  long long res = a[n - 1] + s + 1;
  cout << res / 60 << " " << res % 60 << endl;
  return 0;
}
