#include <bits/stdc++.h>
using namespace std;
const int maxn = 10000 + 100;
long long MOD;
int main() {
  int n, s;
  scanf("%d %d", &n, &s);
  int a[maxn];
  for (int i = 0; i < n; i++) {
    int h, m;
    scanf("%d %d", &h, &m);
    a[i] = 60 * h + m;
  }
  if (a[0] > s) {
    printf("%d %d\n", 0, 0);
    return 0;
  }
  for (int i = 0; i < n; i++) {
    if (i != n - 1) {
      if (a[i + 1] - a[i] > 2 * s + 1) {
        int x = a[i] + s + 1;
        printf("%d %d\n", x / 60, x % 60);
        return 0;
      }
    } else {
      int x = a[i] + s + 1;
      printf("%d %d\n", x / 60, x % 60);
      return 0;
    }
  }
  return 0;
}
