#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-3;
const int MAXN = 2e5 + 10;
const int MAXM = 2e6 + 10;
const long long mod = 1e9;
int h[110], m[110];
int main() {
  int n, s;
  scanf("%d%d", &n, &s);
  for (int i = 0; i < n; i++) scanf("%d%d", &h[i], &m[i]);
  if ((h[0] * 60 + m[0]) > s) {
    puts("0 0");
    return 0;
  }
  for (int i = 1; i < n; i++) {
    int num = (h[i] - h[i - 1]) * 60 + m[i] - m[i - 1];
    if (num > (2 * s + 1)) {
      int ans1 = h[i - 1], ans2 = m[i - 1] + s + 1;
      if (ans2 >= 60) ans1 += ans2 / 60, ans2 %= 60;
      printf("%d %d\n", ans1, ans2);
      return 0;
    }
  }
  int ans1 = h[n - 1], ans2 = m[n - 1] + s + 1;
  if (ans2 >= 60) ans1 += ans2 / 60, ans2 %= 60;
  printf("%d %d\n", ans1, ans2);
  return 0;
}
