#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int n, s;
  cin >> n >> s;
  vector<pair<int, int>> vp;
  for (int i = 0; i < n; i++) {
    int h, m;
    cin >> h >> m;
    vp.push_back(make_pair(h, m));
  }
  int t;
  t = (vp[0].first * 60) + vp[0].second;
  if (t >= s + 1) {
    cout << 0 << " " << 0;
    return 0;
  }
  int h, m;
  for (int i = 1; i < n; i++) {
    int ct = vp[i].first * 60 + vp[i].second;
    if (ct - t >= 2 * (s + 1)) {
      int rm = t + s + 1;
      h = rm / 60;
      m = rm % 60;
      cout << h << " " << m;
      return 0;
    }
    t = ct;
  }
  int rm = t + s + 1;
  h = rm / 60;
  m = rm % 60;
  cout << h << " " << m;
  return 0;
}
