#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, s;
  cin >> n >> s;
  vector<int> a;
  for (int i = 0; i < n; i++) {
    int u, v;
    cin >> u >> v;
    a.push_back((u * 60 + v));
  }
  int lim = 24 * 60 - 1;
  for (int i = 0; i <= lim + s + 1; i++) {
    int m = i % 60;
    int h = i / 60;
    int k = lower_bound(a.begin(), a.end(), i) - a.begin();
    if (k == n) {
      if (i - a[n - 1] - 1 >= s) {
        cout << h << " " << m << '\n';
        return 0;
      }
    }
    if (k == 0) {
      if (a[0] - i - 1 >= s) {
        cout << h << " " << m << '\n';
        return 0;
      }
    }
    if (a[k] - i - 1 >= s && i - a[k - 1] - 1 >= s) {
      cout << h << " " << m << '\n';
      return 0;
    }
  }
}
