#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long int a, p, q, l, b = 0, i, d, m, n, k, sum = 0, t = 1, x = 1, z,
                            y = 0;
  vector<int> v;
  cin >> t >> a;
  for (int j = 0; j < t; j++) {
    cin >> p >> q;
    k = p * 60 + q;
    v.push_back(k);
  }
  if (v[0] >= (a + 1)) {
    cout << 0 << " " << 0;
    exit(0);
  }
  for (int i = 0; i < t - 1; i++) {
    if ((v[i + 1] - v[i]) >= (2 * a + 2)) {
      k = v[i] + a + 1;
      cout << k / 60 << " " << k % 60;
      exit(0);
    }
  }
  if ((v[t - 1] - v[0]) >= (2 * a + 2)) {
    k = v[t - 1] + a + 1;
    cout << k / 60 << " " << k % 60;
    exit(0);
  }
  k = v[t - 1] + a + 1;
  cout << k / 60 << " " << k % 60;
  return 0;
}
