#include <bits/stdc++.h>
using namespace std;
pair<long long, long long> arr[100100];
int main() {
  long long n, second, i;
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> second;
  long long req = (second + 1);
  for (i = 1; i <= n; i++) {
    cin >> arr[i].first >> arr[i].second;
  }
  sort(arr + 1, arr + n + 1);
  for (i = 1; i <= n; i++) {
    long long h = arr[i].first - arr[i - 1].first;
    long long min = arr[i].second - arr[i - 1].second;
    if (min < 0) h--, min += 60;
    min += (h * 60);
    if (min >= req * 2 || (i == 1 && min >= req)) {
      if (i == 1 && min >= req) {
        cout << 0 << ' ' << 0 << '\n';
        return 0;
      }
      long long op1 = arr[i - 1].first;
      long long op2 = arr[i - 1].second;
      while (req--) {
        op2++;
        if (op2 >= 60) op1++, op2 = 0;
      }
      cout << op1 << ' ' << op2 << '\n';
      return 0;
    }
  }
  long long op1 = arr[n].first;
  long long op2 = arr[n].second;
  while (req--) {
    op2++;
    if (op2 >= 60) op1++, op2 = 0;
  }
  cout << op1 << ' ' << op2 << '\n';
  return 0;
}
