#include <bits/stdc++.h>
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
char _;
inline long long powmod(long long x, long long n, long long _mod) {
  long long res = 1;
  while (n) {
    if (n & 1) res = (res * x) % _mod;
    x = (x * x) % _mod;
    n >>= 1;
  }
  return res;
}
int n, s;
int h[200];
int m[200];
int t[200];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> s;
  for (int i = 0; i < n; ++i) {
    cin >> h[i] >> m[i];
    t[i] = h[i] * 60 + m[i];
  }
  for (int waqt = 0;; waqt++) {
    bool ok = 1;
    for (int j = 0; j < n; ++j) {
      if (t[j] == waqt) ok = 0;
      if (t[j] < waqt and abs(t[j] + 1 - waqt) < s) ok = 0;
      if (t[j] > waqt and abs(t[j] - (waqt + 1)) < s) ok = 0;
    }
    if (ok) {
      int hr = waqt / 60;
      int min = waqt - (hr * 60);
      cout << hr << ' ' << min << '\n';
      return 0;
    }
  }
  return 0;
}
