#include <bits/stdc++.h>
using namespace std;
const int MAX = 105;
int n, s;
int h[MAX], m[MAX], t[MAX];
int main() {
  cin >> n >> s;
  t[0] = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> h[i] >> m[i];
    t[i] = h[i] * 60 + m[i];
  }
  if (t[1] >= s + 1) {
    cout << 0 << " " << 0 << endl;
    return 0;
  }
  bool flag = false;
  for (int i = 2; i <= n; ++i) {
    if (t[i] - t[i - 1] >= 2 * (s + 1)) {
      flag = 1;
      cout << (t[i - 1] + s + 1) / 60 << " " << (t[i - 1] + s + 1) % 60;
      break;
    }
  }
  if (!flag) cout << (t[n] + s + 1) / 60 << " " << (t[n] + s + 1) % 60;
  return 0;
}
