#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  setprecision(10);
  std::cout << std::fixed;
  std::cout << std::setprecision(10);
  int n, s;
  cin >> n >> s;
  int x = 0, y = 0;
  for (int i = 0; i < n; i++) {
    int h, m;
    cin >> h >> m;
    if (i == 0) {
      int gap = 60 * (h - x) + (m - y);
      if (gap >= (s + 1)) {
        cout << 0 << " " << 0;
        return (0);
      }
      x = h;
      y = m;
    } else {
      int gap = 60 * (h - x) + (m - y);
      if (gap >= (2 * s + 2)) {
        int num2 = (y + s + 1) % 60;
        int num1 = (x) + (y + s + 1) / 60;
        cout << num1 << " " << num2;
        return (0);
      }
      x = h;
      y = m;
    }
  }
  x = x + (y + s + 1) / 60;
  y = (y + s + 1) % 60;
  cout << x << " " << y;
}
