#include <bits/stdc++.h>
using namespace std;
long long n, s, a[111111], b[111111], i, ans, x[111111];
int main() {
  cin >> n >> s;
  for (i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
    x[i] = a[i] * 60 + b[i];
  }
  if (x[1] >= s + 1) {
    cout << 0 << " " << 0;
    return 0;
  }
  for (i = 1; i <= n; i++) {
    if (x[i] - x[i - 1] > 2 * s + 1) {
      ans = x[i - 1] + 1 + s;
      cout << ans / 60 << " " << ans % 60;
      return 0;
    }
  }
  ans = x[i - 1] + 1 + s;
  cout << ans / 60 << " " << ans % 60;
}
