#include <bits/stdc++.h>
using namespace std;
int n, s;
int a[2500];
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> s;
  for (int i = 0; i < n; i++) {
    int h, m;
    cin >> h >> m;
    a[h * 60 + m] = 1;
  }
  for (int i = 0; i < 2500; i++) {
    bool ok = true;
    for (int j = i; j >= 0 && j >= i - s; j--) {
      if (a[j]) ok = false;
    }
    for (int j = i; j < 2500 && j <= i + s; j++) {
      if (a[j]) ok = false;
    }
    if (ok) {
      cout << i / 60 << ' ' << i % 60 << endl;
      exit(0);
    }
  }
}
