#include <bits/stdc++.h>
using namespace std;
const int N = int(1e5) + 500;
int n, m, a[N], s, h, mm;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> s;
  for (int i = 1; i <= n; i++) {
    cin >> h >> mm;
    a[++m] = h * 60 + mm;
  }
  if (a[1] >= s + 1) {
    cout << 0 << " " << 0;
    exit(0);
  }
  for (int i = 2; i <= n; i++) {
    if (a[i] - a[i - 1] >= s * 2 + 2) {
      cout << (a[i - 1] + s + 1) / 60 << ' ' << (a[i - 1] + s + 1) % 60;
      exit(0);
    }
  }
  cout << (a[m] + s + 1) / 60 << " " << (a[m] + s + 1) % 60;
  return 0;
}
