#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 7;
const int inf = INT_MAX;
const int mod = 1e9 + 7;
int a[maxn];
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, s;
  cin >> n >> s;
  a[0] = 0;
  for (int i = 1; i <= n; i++) {
    int h, m;
    cin >> h >> m;
    a[i] = h * 60 + m;
  }
  a[n + 1] = inf;
  if (a[1] - a[0] >= s + 1) {
    cout << 0 << ' ' << 0 << endl;
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    if (a[i + 1] - a[i] >= 2 * s + 2) {
      int now = a[i] + 1 + s;
      cout << now / 60 << ' ' << now % 60 << endl;
      return 0;
    }
  }
}
