#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
const int P = 1000000007;
const int INF = 0x3f3f3f3f;
const long long LLINF = 0x3f3f3f3f3f3f3f3f;
const long double eps = 1e-6;
int main() {
  int n, k;
  cin >> n >> k;
  pair<int, int> a[105];
  for (int i = (1); i <= (n); ++i) cin >> a[i].first >> a[i].second;
  int time = a[1].first * 60 + a[1].second;
  if (time >= k + 1) return 0 * printf("0 0\n");
  for (int i = (1); i <= (n - 1); ++i) {
    time = (a[i + 1].first * 60) + a[i + 1].second -
           (a[i].first * 60 + a[i].second);
    int t = a[i].first * 60 + a[i].second + k + 1;
    if (time >= 2 * k + 2) return 0 * printf("%d %d\n", t / 60, t % 60);
  }
  int t = a[n].first * 60 + a[n].second + k + 1;
  printf("%d %d\n", t / 60, t % 60);
  return 0;
}
