#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline void write(T x) {
  int i = 20;
  char buf[21];
  buf[20] = '\n';
  do {
    buf[--i] = x % 10 + '0';
    x /= 10;
  } while (x);
  do putchar(buf[i]);
  while (buf[i++] != '\n');
}
template <typename T>
inline T readInt() {
  T n = 0, s = 1;
  char p = getchar();
  if (p == '-') s = -1;
  while ((p < '0' || p > '9') && p != EOF && p != '-') p = getchar();
  if (p == '-') s = -1, p = getchar();
  while (p >= '0' && p <= '9') {
    n = (n << 3) + (n << 1) + (p - '0');
    p = getchar();
  }
  return n * s;
}
long long int t[101];
int main() {
  long long int n, s, t0;
  n = readInt<long long int>();
  s = readInt<long long int>();
  for (long long int i = 1; i <= n; i += 1) {
    long long int h, m;
    h = readInt<long long int>();
    m = readInt<long long int>();
    t[i] = 60 * h + m;
  }
  t0 = 0;
  while (1) {
    bool ok;
    ok = true;
    for (long long int i = 1; i <= n; i += 1)
      if (abs(t0 - t[i]) <= s) ok = false;
    if (ok) {
      cout << t0 / 60 << ' ' << t0 % 60 << '\n';
      return 0;
    }
    t0++;
  }
  return 0;
}
