#include <bits/stdc++.h>
using namespace std;
vector<int> v;
int main() {
  int n, s, x, h, m;
  cin >> n >> s;
  for (int i = 0; i < n; i++) {
    cin >> h >> m;
    v.push_back((h * 60) + m);
  }
  int take = (s * 2) + 2;
  if (v[0] >= s + 1) {
    cout << 0 << " " << 0 << endl;
    return 0;
  }
  int ans = v[0];
  for (int i = 1; i < n; i++) {
    if (ans + take <= v[i]) {
      int res = (ans + s + 1);
      int hh = (res / 60);
      int haha = res - (hh * 60);
      cout << hh << " " << haha << endl;
      return 0;
    }
    ans = v[i];
  }
  int res = (v[n - 1]) + s + 1;
  int hh = (res / 60);
  int haha = res - (hh * 60);
  cout << hh << " " << haha << endl;
  return 0;
}
