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