#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 5;
int a[N], b[N];
int main() {
  int n, s;
  cin >> n >> s;
  for (int i = 1; i <= n; i++) cin >> a[i] >> b[i];
  for (int i = 1; i <= n; i++) {
    if (i == 1) {
      if (a[i] * 60 + b[i] >= s + 1) {
        printf("0 0\n");
        break;
      }
    }
    if (i == n) {
      if (b[i] + s + 1 >= 60) a[i] += (b[i] + s + 1) / 60;
      b[i] = (b[i] + s + 1) % 60;
      printf("%d %d\n", a[i], b[i]);
      break;
    }
    int t = a[i + 1] - a[i];
    t = t * 60 + b[i + 1] - b[i];
    if (t >= 2 * s + 2) {
      if (b[i] + s + 1 >= 60) a[i] += (b[i] + s + 1) / 60;
      b[i] = (b[i] + s + 1) % 60;
      printf("%d %d\n", a[i], b[i]);
      break;
    }
  }
  return 0;
}
