#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int MAX = 1e5 + 5;
int n, s, h, m;
vector<pair<int, int> > a;
int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(false);
  cin >> n >> s;
  for (int i = 0; i < n; i++) {
    cin >> h >> m;
    a.push_back(make_pair(h, m));
  }
  bool done = false;
  if (a[0].first * 60 + a[0].second >= s + 1)
    cout << "0 0";
  else {
    for (int i = 0; i < n - 1; i++) {
      int gap =
          (a[i + 1].first - a[i].first) * 60 + (a[i + 1].second - a[i].second);
      if (gap >= 2 * (s + 1)) {
        done = true;
        if (a[i].second + s + 1 >= 60) {
          int mins = s + 1 - (60 - a[i].second);
          if (mins == 60)
            cout << a[i].first + 2 << " 0";
          else
            cout << a[i].first + 1 << " " << mins;
        } else
          cout << a[i].first << " " << a[i].second + s + 1;
        break;
      }
    }
    if (!done) {
      if (a[n - 1].second + s + 1 >= 60) {
        int mins = s + 1 - (60 - a[n - 1].second);
        if (mins == 60)
          cout << a[n - 1].first + 2 << " 0";
        else
          cout << a[n - 1].first + 1 << " " << mins;
      } else
        cout << a[n - 1].first << " " << a[n - 1].second + s + 1;
    }
  }
  return 0;
}
