#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int MAXN = 1e6 + 5, inf = 1e9;
const ll INF = 1e18;
const ld PI = 3.1415926535897932384626433832795;
int cnt[MAXN];
ll dp[MAXN], tab[MAXN];
bool vis[MAXN], used[MAXN];
vector<int> G[MAXN];
vector<ll> V;
set<ll> S;
map<ll, int> M;
stack<ll> St;
queue<ll> Q;
void prt(int tim) {
  int h = tim / 60;
  tim %= 60;
  cout << h << " " << tim << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout << setprecision(13) << fixed;
  int n, s;
  cin >> n >> s;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    V.push_back(a * 60 + b);
    V.push_back(a * 60 + b + 1);
    V.push_back(a * 60 + b - 1);
  }
  V.push_back(-s);
  sort(V.begin(), V.end());
  V.push_back(inf);
  for (int i = 0; i < V.size(); i++) {
    if (V[i + 1] - V[i] >= 2 * s) {
      prt(V[i] + s);
      return 0;
    }
  }
}
