#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, s;
  cin >> n >> s;
  long long a[n + 1];
  for (long long i = 0; i < n; i++) {
    long long h, m;
    cin >> h >> m;
    a[i] = 60 * h + m;
  }
  a[n] = 26 * 60 + 5;
  if (s + 1 <= a[0]) {
    cout << 0 << " " << 0;
    return 0;
  }
  for (long long i = 0; i < n; i++) {
    if (a[i] + 2 * s + 2 <= a[i + 1]) {
      long long h = (a[i] + s + 1) / 60, m = (a[i] + s + 1) % 60;
      cout << h << " " << m;
      return 0;
    }
  }
}
