#include <bits/stdc++.h>
using namespace std;
int t[101];
int main() {
  int n, s;
  scanf("%d%d", &n, &s);
  int a, b;
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &a, &b);
    t[i] = a * 60 + b;
  }
  int lim = 2 * s + 2, tim;
  if (t[1] >= s + 1)
    printf("0 0");
  else {
    for (int i = 1; i < n; ++i)
      if (t[i + 1] - t[i] >= lim) {
        tim = t[i] + 1 + s;
        printf("%d %d", tim / 60, tim % 60);
        return 0;
      }
    tim = t[n] + s + 1;
    printf("%d %d", tim / 60, tim % 60);
  }
  return 0;
}
