#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, s, x, y, a = 0, b = 0, h, m;
  cin >> n >> s;
  for (int i = 0; i < n; i++) {
    cin >> h >> m;
    x = a * 60 + b;
    y = h * 60 + m;
    if (i == 0 && x == 0) {
      if (s + 1 <= y) {
        cout << 0 << " " << 0;
        return 0;
      }
    }
    if (x + 2 * s + 2 <= y) {
      x += s + 1;
      cout << x / 60 << " " << x % 60;
      return 0;
    } else {
      a = h;
      b = m;
    }
  }
  x = a * 60 + b + s + 1;
  cout << x / 60 << " " << x % 60;
  return 0;
}
