#include <bits/stdc++.h>
using namespace std;
ifstream fin("input.txt");
ofstream fout("output.txt");
bool isPrime(int x) {
  if (x <= 4 || x % 2 == 0 || x % 3 == 0) return x == 2 || x == 3;
  for (int i = 5; i * i <= x; i += 6)
    if (x % i == 0 || x % (i + 2) == 0) return 0;
  return 1;
}
inline void boostIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int main() {
  boostIO();
  int n, s;
  cin >> n >> s;
  int prev = 0;
  int h = 0;
  int m = 0;
  for (int i = 0; i < (n); ++i) {
    cin >> h >> m;
    m += h * 60;
    if (i == 0 && m - prev >= s + 1) {
      cout << 0 << " " << 0 << endl;
      return 0;
    }
    if (m - prev >= s * 2 + 2) {
      cout << (prev + s + 1) / 60 << " " << (prev + s + 1) % 60 << endl;
      return 0;
    }
    prev = m;
  }
  cout << (prev + s + 1) / 60 << " " << (prev + s + 1) % 60;
  return 0;
}
