#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
void solve() {
  long long n, s, h, m;
  cin >> n >> s;
  long long tim = -s;
  for (long long i = 0; i < n; ++i) {
    cin >> h >> m;
    m += h * 60;
    if (tim + 2 * s + 1 <= m) {
      break;
    }
    tim = m + 1;
  }
  cout << (tim + s) / 60 << ' ' << (tim + s) % 60 << endl;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  for (long long i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}
