2 10 1 1 1
1
10
1
1 5 1 8
 4 4 1 0 1
1

1
1 2 1 4
 2 5 1 0 1
2

1
1 4 1 5
 10 10 1 8 4
10
2 3 4 5 6 7 8 9
10
1 1 3 1
2 1 7 1
1 1 9 1
7 1 4 1
10 1 7 1
2 1 7 1
3 1 2 1
5 1 2 1
10 1 5 1
6 1 9 1
 2 4 1 1 1
1
4
1
1 2 1 3
 2 2 0 1 1

1
1
1 2 2 2
 1000 1000 1 1 10
1
2
1
1 900 1 1000
 5 5 1 1 1
3
2
1
1 5 1 1
 2 4 1 1 1
1
2
1
2 3 2 4
 4 4 1 0 1
4

5
1 1 2 2
1 3 2 2
3 3 4 3
3 2 2 2
1 2 2 3

3
 2
 1
 3
4
4
3
3
4
3
3
4
3
 1
 3
 100
 4
 1
 6
4
3
5
4

#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-8;
const long long mod = long long(1e9 + 7);
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
void run() {}
void stop() {}
const double PI = acos(-1.0);
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, s;
  cin >> n >> s;
  int h = 0, m = 0;
  int f = 0;
  while (n--) {
    int h1, m1;
    cin >> h1 >> m1;
    if (!f && h1 * 60 + m1 >= s + 1) {
      cout << 0 << " " << 0 << '\n';
      return 0;
    }
    if (abs(h1 * 60 + m1 - h * 60 - m) >= 2 * s + 2) {
      cout << (h * 60 + m + s + 1) / 60 << " " << (h * 60 + m + s + 1) % 60
           << '\n';
      return 0;
    }
    h = h1;
    m = m1;
    f = 1;
  }
  cout << (h * 60 + s + m) / 60 << " " << (h * 60 + s + m + 1) % 60;
  stop();
}
