#include <bits/stdc++.h>
using namespace std;
const long long int INF = 1e18;
const int inf = 1e9;
const int MOD = 1e9 + 7;
const int nax = 1000000 + 10;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, s;
  cin >> n >> s;
  int a, b, c, d;
  cin >> a >> b;
  if (a * 60 + b >= s + 1) {
    cout << 0 << " " << 0;
    return 0;
  }
  for (int i = 1; i < n; i++) {
    cin >> c >> d;
    if (a == c) {
      int m = d - b;
      if (m >= 2 * s + 2) {
        int k = b + s + 1;
        int j = k / 60;
        int f = k % 60;
        cout << a + j << " " << f;
        return 0;
      }
    } else {
      int m = c - a - 1;
      m *= 60;
      m += 60 - b;
      m += d;
      if (m >= 2 * s + 2) {
        int k = b + s + 1;
        int j = k / 60;
        int f = k % 60;
        cout << a + j << " " << f;
        return 0;
      }
    }
    a = c, b = d;
  }
  int k = b + s + 1;
  int j = k / 60;
  int f = k % 60;
  cout << a + j << " " << f;
  return 0;
}
