#include <bits/stdc++.h>
using namespace std;
int hh, mm, hht, mmt;
void fun(int h1, int m1, int h2, int m2) {
  if (m1 < m2 && !h1) {
    hh = 0;
    mm = 0;
  } else {
    if (m1 >= m2) {
      mm = m1 - m2;
    } else {
      mm = (60 + m1) - m2;
      h1--;
    }
    hh = h1 - h2;
  }
  return;
}
void fun1(int h1, int m1, int h2, int m2) {
  mmt = (m1 + m2) % 60;
  hht = (h1 + h2) + ((m1 + m2) / 60);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, s;
  cin >> n >> s;
  vector<pair<int, int> > tt;
  int h1, m1, h2, m2;
  for (int i = 0; i < n; i++) {
    cin >> h1 >> m1;
    tt.push_back({h1, m1});
  }
  int hi, mi;
  int ht, mt;
  s++;
  hi = (s / 60);
  mi = (s - ((s / 60) * 60));
  s--;
  fun(tt[0].first, tt[0].second, 0, 0);
  if (hh >= hi && mm >= mi || (hh * 60 + mm >= s + 1)) {
    cout << "0 0" << endl;
    return 0;
  }
  s = (s * 2) + 2;
  ht = s / 60;
  mt = s - ((s / 60) * 60);
  int f = false;
  for (int i = 0; i < n - 1; i++) {
    int j = i + 1;
    fun(tt[j].first, tt[j].second, tt[i].first, tt[i].second);
    if ((hh * 60 + mm) >= s) {
      fun1(tt[i].first, tt[i].second, hi, mi);
      cout << hht << " " << mmt << endl;
      f = true;
      return 0;
    }
  }
  s--;
  ht = s / 60;
  mt = s - ((s / 60) * 60);
  fun1(tt[n - 1].first, tt[n - 1].second, hi, mi);
  cout << hht << " " << mmt << endl;
  return 0;
}
