#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 7, inf = 1e9 + 7;
int n, s, m, d;
int tm(int a, int b) { return a * 60 + b; }
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> s;
  int ans = inf;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    int b, c;
    cin >> b >> c;
    a[i] = tm(b, c);
    if (i && a[i - 1] + 2 * s + 1 < a[i]) {
      ans = min(a[i - 1] + s + 1, ans);
    }
  }
  if (a[0] > s) ans = 0;
  ans = min(ans, a.back() + s + 1);
  cout << ans / 60 << " " << ans % 60 << " ";
  return 0;
}
