#include <bits/stdc++.h>
using namespace std;
int main() {
  long long k, n, s, p;
  long long ans;
  while (cin >> n >> s) {
    vector<long long> t;
    t.push_back(-s - 1);
    bool find = false;
    for (long long i = 0; i < n; ++i) {
      long long h, m;
      cin >> h >> m;
      long long last_t = t[t.size() - 1];
      t.push_back(h * 60 + m);
      if (!find && t[t.size() - 1] - last_t >= 2 * s + 2) {
        ans = last_t + s + 1;
        find = true;
      }
    }
    if (!find) ans = t[t.size() - 1] + s + 1;
    cout << ans / 60 << " " << ans % 60 << endl;
  }
  return 0;
}
