#include <bits/stdc++.h>
using namespace std;
int n, t, ansh, anss;
int h[110], s[110];
int main() {
  cin >> n >> t;
  for (int i = 0; i < n; i++) cin >> h[i] >> s[i];
  ansh = 0;
  anss = 0;
  for (int i = 0; i < n; i++) {
    if ((h[i] - ansh) * 60 + s[i] - anss >= t + 1) break;
    anss = s[i] + t + 1;
    ansh = h[i];
    if (anss >= 60) {
      ansh += anss / 60;
      anss -= (anss / 60) * 60;
    }
  }
  cout << ansh << ' ' << anss << endl;
  return 0;
}
