#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, s;
  cin >> n >> s;
  int a[n][2], i, sum[n], x = -2;
  int b, c;
  for (i = 0; i < n; i++) {
    cin >> a[i][0] >> a[i][1];
    sum[i] = a[i][0] * 60 + a[i][1];
  }
  if (1440 - sum[n - 1] >= (s + 1) && sum[0] >= s + 1)
    x = -1;
  else {
    for (i = 0; i < n - 1; i++) {
      if ((sum[i + 1] - sum[i]) >= (2 * s + 2)) {
        x = i;
        break;
      }
    }
    b = (sum[x] + 1 + s) / 60;
    c = (sum[x] + 1 + s) % 60;
  }
  if (x == -2) {
    b = (sum[n - 1] + 1 + s) / 60;
    c = (sum[n - 1] + 1 + s) % 60;
  } else if (x == -1) {
    b = 0;
    c = 0;
  }
  cout << b << ' ' << c;
  return 0;
}
