#include <bits/stdc++.h>
using namespace std;
long long int a[105], b[105];
int main() {
  int ii, i, j, k, n, m, z = 0, c = 0, t, x, y, l, r, mid;
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  a[n] = a[n - 1] + 5;
  b[n] = 0;
  m++;
  x = a[0] * 60 + b[0];
  if (x >= (m)) {
    cout << 0 << " " << 0 << endl;
    return 0;
  }
  for (i = 1; i <= n; i++) {
    x = (a[i] * 60 + b[i]) - (a[i - 1] * 60 + b[i - 1]);
    if (x >= (2 * m)) {
      x = a[i - 1] * 60 + b[i - 1] + m;
      cout << x / 60 << " " << x % 60 << endl;
      return 0;
    }
  }
}
