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