#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  int n, s;
  while (cin >> n >> s) {
    int hr, min;
    int resh = -1, resm = -1;
    int tmph = 0, tmpm = 0;
    int cnt = 0;
    for (int i = 0; i != n; i++) {
      cin >> hr >> min;
      cnt++;
      if (resh == -1) {
        if (cnt == 1) {
          if (hr * 60 + min > s) {
            resh = 0;
            resm = 0;
          }
        } else if ((hr - tmph) * 60 + (min - tmpm) > 2 * s + 1) {
          int t = (tmpm + 1 + s) / 60;
          resm = (tmpm + 1 + s) % 60;
          resh = tmph + t;
        }
      }
      tmph = hr;
      tmpm = min;
    }
    if (resh == -1) {
      int t = (min + 1 + s) / 60;
      resm = (min + 1 + s) % 60;
      resh = hr + t;
    }
    cout << resh << " " << resm << endl;
  }
}
