#include <bits/stdc++.h>
using namespace std;
int a[101], b[101];
int main() {
  int n, m;
  cin >> n >> m;
  int i, j, k;
  int p = 0, q = 0, f = 0;
  for (i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
  }
  for (i = 1; i <= n; i++) {
    k = (a[i] * 60 + b[i]) - (a[i - 1] * 60 + b[i - 1]);
    if (i == 1 && k >= (m + 1)) {
      p = 0, q = 0;
      f = 1;
      break;
    }
    if (k >= (2 * m + 2) && i > 1) {
      k = a[i - 1] * 60 + b[i - 1] + m + 1;
      p = k / 60;
      q = k % 60;
      f = 1;
      break;
    }
  }
  if (f == 1) {
    cout << p << " " << q;
  } else {
    k = (a[n] * 60 + b[n]) + m + 1;
    p = k / 60;
    q = k % 60;
    cout << p << " " << q;
  }
}
