#include <bits/stdc++.h>
using namespace std;
int n, m;
bool pass[1500 + 10];
signed main() {
  memset(pass, 1, sizeof(pass));
  scanf("%d%d", &n, &m);
  int x, y;
  for (register int(i)(0); (i) ^ (n); (i)++) {
    scanf("%d%d", &x, &y);
    int cnt((x * 60) + y);
    for (register int(j)((max(0, (cnt - m)))); (j) ^ ((cnt + m + 1)); (j)++) {
      pass[j] = 0;
    }
  }
  for (register int(i)(0); (i) ^ ((1500 + 1)); (i)++) {
    if (pass[i]) {
      printf("%d %d", (i / 60), (i % 60));
      break;
    }
  }
  return putchar((10)), 0;
}
