#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, s;
  cin >> n >> s;
  long long a[1111];
  for (int i = 1; i <= n; i++) {
    long long aa, bb;
    cin >> aa >> bb;
    a[i] = aa * 60 + bb;
  }
  long long i = 0;
  a[0] = -10000;
  a[n + 1] = 1e9;
  long long j = 0;
  while (true) {
    if (i == a[j + 1]) {
      j++;
    }
    if (abs(i - a[j]) > s && abs(a[j + 1] - i) > s) {
      cout << i / 60 << " " << i % 60 << endl;
      return 0;
    }
    i++;
  }
  return 0;
}
