#include <bits/stdc++.h>
using namespace std;
int n, s;
int h[1005], m[1005];
int main() {
  int i, j;
  cin >> n >> s;
  for (i = 1; i <= n; i++) {
    cin >> h[i] >> m[i];
    int hh = h[i - 1] + (m[i - 1] + s + 1) / 60;
    int mm = (m[i - 1] + s + 1) % 60;
    if (i == 1) {
      hh = 0;
      mm = 0;
    }
    int h1 = hh + (mm + s + 1) / 60;
    int m1 = (mm + s + 1) % 60;
    if (h1 < h[i]) {
      cout << hh << ' ' << mm << '\n';
      return 0;
    } else if (h1 == h[i]) {
      if (m1 <= m[i]) {
        cout << hh << ' ' << mm << '\n';
        return 0;
      }
    }
  }
  int hh = h[n] + (m[n] + s + 1) / 60;
  int mm = (m[n] + s + 1) % 60;
  cout << hh << ' ' << mm << '\n';
  return 0;
  return 0;
}
