#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const int MAX = 100005;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, s;
  cin >> n >> s;
  n--;
  int h = 0, m = 0;
  cin >> h >> m;
  if (s + 1 <= h * 60 + m) cout << "0 0" << endl, exit(0);
  while (n--) {
    int hh, mm;
    cin >> hh >> mm;
    int tmp = 0;
    if (mm >= m)
      tmp = (hh - h) * 60 + (mm - m);
    else
      tmp = (hh - h - 1) * 60 + 60 - m + mm;
    if (tmp >= s * 2 + 2) break;
    h = hh;
    m = mm;
  }
  m += s;
  m++;
  if (m >= 60) m -= 60, h++;
  if (m >= 60) m -= 60, h++;
  cout << h << " " << m << endl;
  return 0;
}
