#include <bits/stdc++.h>
using namespace std;
int n, s;
int h[101], m[101];
int sum1, sum2;
int main(void) {
  cin >> n >> s;
  int cnt = 2 * s + 2;
  for (int i = 1; i <= n; i++) {
    cin >> h[i] >> m[i];
  }
  for (int i = 1; i <= n; i++) {
    if (i == 1) {
      sum2 = h[i] * 60 + m[i];
      if (sum2 - sum1 >= (s + 1)) {
        cout << "0"
             << " "
             << "0" << endl;
        return 0;
      }
    } else {
      sum2 = h[i] * 60 + m[i];
      if (sum2 - sum1 >= cnt) {
        int z;
        z = sum1 + s + 1;
        cout << z / 60 << " " << z % 60 << endl;
        return 0;
      }
    }
    sum1 = sum2;
  }
  int z;
  z = sum1 + s + 1;
  cout << z / 60 << " " << z % 60 << endl;
  return 0;
}
