#include <bits/stdc++.h>
using namespace std;
mt19937 gen(chrono::system_clock::now().time_since_epoch().count());
const int M = 1e9 + 7;
const int N = 1e6 + 7;
int n, s, h, m;
vector<int> lol;
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 >> m;
    lol.push_back(h * 60 + m);
  }
  sort(lol.begin(), lol.end());
  if (lol[0] >= s + 1) {
    cout << 0 << ' ' << 0;
    return 0;
  }
  for (int i = 0; i < n - 1; i++)
    if (lol[i + 1] > lol[i] + 2 * s + 1) {
      cout << (lol[i] + s + 1) / 60 << ' ' << (lol[i] + s + 1) % 60;
      return 0;
    }
  cout << (lol.back() + s + 1) / 60 << ' ' << (lol.back() + s + 1) % 60;
  return 0;
}
