#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, s;
  cin >> n >> s;
  int t[100];
  for (int i = 0; i < n; i++) {
    int h, m;
    cin >> h >> m;
    t[i] = h * 60 + m;
  }
  if (t[0] >= s + 1) {
    cout << "0 0" << '\n';
  } else {
    for (int i = 1; i < n; i++) {
      if (t[i - 1] + 2 * s + 2 <= t[i]) {
        int time = t[i - 1] + s + 1;
        cout << time / 60 << " " << time % 60 << '\n';
        return 0;
      }
    }
    int time = t[n - 1] + s + 1;
    cout << time / 60 << " " << time % 60 << '\n';
  }
  return 0;
}
