#include <bits/stdc++.h>
using namespace std;
inline long long fexp(long long b, long long e) {
  long long x = 1;
  for (; e; e >>= 1, b = (b * b) % (1000000007))
    if (e & 1) x = (x * b) % (1000000007);
  return x;
}
long long n, s, t[103], h, m;
signed main() {
  cin >> n >> s;
  t[0] = -(s + 1);
  for (long long i = 1; i <= n; i++) {
    cin >> h >> m;
    t[i] = h * 60 + m;
  }
  t[n + 1] = (1000000000);
  for (long long i = 0; i <= n; i++) {
    if (t[i + 1] - t[i] > 2 * s + 1) {
      long long k = t[i] + s + 1;
      cout << k / 60 << ' ' << k % 60 << endl;
      return 0;
    }
  }
  return 0;
}
