#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, s, h, m, x, c, i, j, ans = 0;
  cin >> n >> s;
  for (i = 1; i <= n; i++) {
    cin >> h >> m;
    x = h * 60 + m;
    if (ans + s < x) {
      break;
    } else
      ans = x + s + 1;
  }
  cout << ans / 60 << " " << ans % 60 << endl;
}
