#include <bits/stdc++.h>
using namespace std;
const long double eps = 1e-8;
const double pi = acos(-1.0);
const int mod = 1000000007;
const int INF = 0x3f3f3f3f;
const int maxn = 1e6 + 20;
int n, s;
int k[105];
int main() {
  cin >> n >> s;
  int h, m;
  for (int i = 1; i <= n; i++) {
    cin >> h >> m;
    k[i] = h * 60 + m;
  }
  int ans = 0;
  if (k[1] >= s + 1) {
    cout << "0 0" << endl;
    return 0;
  }
  bool flag = 0;
  for (int i = 2; i <= n; i++) {
    if (k[i] - k[i - 1] >= 2 * s + 2) {
      ans = k[i - 1] + s + 1;
      flag = 1;
      break;
    }
  }
  if (flag) {
    cout << ans / 60 << " " << ans % 60 << endl;
  } else {
    ans = k[n] + s + 1;
    cout << ans / 60 << " " << ans % 60 << endl;
  }
  return 0;
}
