#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, s, ans, ans2, ans3, min2, min1;
  cin >> n >> s;
  int time = 2 * s + 2;
  vector<pair<int, int> > a;
  for (int i = 0; i < n; i++) {
    int h, m;
    cin >> h >> m;
    a.push_back(make_pair(h, m));
  }
  if (n == 1) {
    if (a[0].first == 0 && a[0].second == 0) {
      min1 = 60 * a[0].first + a[0].second;
      ans = min1 + s + 1;
      ans2 = ans / 60;
      ans3 = abs(ans2 * 60 - ans);
      cout << ans2 << " " << ans3 << endl;
    } else if ((60 * a[0].first + a[0].second) >= s + 1)
      cout << "0"
           << " "
           << "0";
    else {
      min1 = 60 * a[0].first + a[0].second;
      ans = min1 + s + 1;
      ans2 = ans / 60;
      ans3 = abs(ans2 * 60 - ans);
      cout << ans2 << " " << ans3 << endl;
    }
    return 0;
  }
  if ((60 * a[0].first + a[0].second) >= s + 1) {
    cout << "0"
         << " "
         << "0";
    return 0;
  }
  for (int i = 1; i < n; i++) {
    min1 = 60 * a[i - 1].first + a[i - 1].second;
    min2 = 60 * a[i].first + a[i].second;
    if (abs(min1 - min2) >= time) {
      ans = min1 + s + 1;
      ans2 = ans / 60;
      ans3 = abs(ans2 * 60 - ans);
      cout << ans2 << " " << ans3 << endl;
      return 0;
    }
  }
  ans = min2 + s + 1;
  ans2 = ans / 60;
  ans3 = abs(ans2 * 60 - ans);
  cout << ans2 << " " << ans3 << endl;
  return 0;
}
