#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return (b == 0) ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); }
long long powmod(long long a, long long b) {
  long long res = 1;
  if (a >= 1000000007) a %= 1000000007;
  for (; b; b >>= 1) {
    if (b & 1) res = res * a;
    if (res >= 1000000007) res %= 1000000007;
    a = a * a;
    if (a >= 1000000007) a %= 1000000007;
  }
  return res;
}
void fast() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
}
void fileIO() {
  freopen("input.in", "r", stdin);
  freopen("output1.txt", "w", stdout);
}
int n, s, h, m;
int t[105];
int main() {
  cin >> n >> s;
  for (int i = 0; i < n; i++) {
    cin >> h >> m;
    t[i] = h * 60 + m;
  }
  sort(t, t + n);
  int ans = -1;
  if (1 + s <= t[0]) {
    ans = 0;
  } else {
    for (int i = 0; i < n - 1; i++) {
      if (t[i] + 1 + s + 1 + s <= t[i + 1]) {
        ans = t[i] + 1 + s;
        break;
      }
    }
  }
  if (ans == -1) {
    cout << (t[n - 1] + 1 + s) / 60 << " " << (t[n - 1] + 1 + s) % 60;
  } else {
    cout << ans / 60 << " " << ans % 60;
  }
  return 0;
}
