#include <bits/stdc++.h>
using namespace std;
long long n, s, a[107], h, m, res;
int main() {
  cin >> n >> s;
  for (int i = 1; i <= n; i++) {
    cin >> h >> m;
    a[i] = h * 60 + m;
  }
  a[0] = -(s + 1);
  a[n + 1] = 100000000000000000;
  for (int i = 0; i <= n; i++) {
    if (a[i + 1] - a[i] > s * 2 + 1) {
      res = a[i] + s + 1;
      cout << res / 60 << ' ' << res % 60 << '\n';
      break;
    }
  }
  return 0;
}
