#include <bits/stdc++.h>
using namespace std;
int tot(int h, int m) { return (60 * h + m); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int i, j, k, n, s;
  cin >> n >> s;
  vector<pair<int, int> > v;
  int day = 0;
  for (int i = 0; i < n; i++) {
    int h, m;
    cin >> h >> m;
    if (i > 0 and h < v[i - 1].first) day++;
    v.push_back(make_pair(day * 24 + h, m));
  }
  if (tot(v[0].first, v[0].second) >= s + 1) {
    cout << "0 0";
    return 0;
  }
  for (int i = 1; i < n; i++) {
    int t1, t2;
    t1 = tot(v[i - 1].first, v[i - 1].second);
    t2 = tot(v[i].first, v[i].second);
    if (t2 - t1 >= 2 * s + 2) {
      int ans = t1 + 1 + s;
      int hh = ans / 60;
      int mm = ans % 60;
      cout << hh << " " << mm;
      return 0;
    }
  }
  int ans = tot(v[n - 1].first, v[n - 1].second) + 1 + s;
  int hh = ans / 60;
  int mm = ans % 60;
  cout << hh << " " << mm;
  return 0;
}
