These days Arkady works as an air traffic controller at a large airport. He controls a runway which is usually used for landings only. Thus, he has a schedule of planes that are landing in the nearest future, each landing lasts $1$ minute.

He was asked to insert one takeoff in the schedule. The takeoff takes $1$ minute itself, but for safety reasons there should be a time space between the takeoff and any landing of at least $s$ minutes from both sides.

Find the earliest time when Arkady can insert the takeoff.


-----Input-----

The first line of input contains two integers $n$ and $s$ ($1 \le n \le 100$, $1 \le s \le 60$) — the number of landings on the schedule and the minimum allowed time (in minutes) between a landing and a takeoff.

Each of next $n$ lines contains two integers $h$ and $m$ ($0 \le h \le 23$, $0 \le m \le 59$) — the time, in hours and minutes, when a plane will land, starting from current moment (i. e. the current time is $0$ $0$). These times are given in increasing order.


-----Output-----

Print two integers $h$ and $m$ — the hour and the minute from the current moment of the earliest time Arkady can insert the takeoff.


-----Examples-----
Input
6 60
0 0
1 20
3 21
5 0
19 30
23 40

Output
6 1

Input
16 50
0 30
1 20
3 0
4 30
6 10
7 50
9 30
11 10
12 50
14 30
16 10
17 50
19 30
21 10
22 50
23 59

Output
24 50

Input
3 17
0 30
1 0
12 0

Output
0 0



-----Note-----

In the first example note that there is not enough time between 1:20 and 3:21, because each landing and the takeoff take one minute.

In the second example there is no gaps in the schedule, so Arkady can only add takeoff after all landings. Note that it is possible that one should wait more than $24$ hours to insert the takeoff.

In the third example Arkady can insert the takeoff even between the first landing.

Difficulty: EASY
Tags: Implementation
Expected Time Complexity: O(N)

Starter code:

Solutions:
(n, s) = map(int, input().split())
a = []
for i in range(n):
	(h, m) = map(int, input().split())
	a.append(60 * h + m)
a.append(10000000000)
if a[0] > s:
	print(0, 0)
else:
	for i in range(n):
		if a[i + 1] - a[i] > 2 * s + 1:
			print((a[i] + s + 1) // 60, (1 + a[i] + s) % 60)
			break


get = lambda : map(int, input().split())
(n, s) = get()
(h, m) = get()
prev = curr = h * 60 + m
if prev >= s + 1:
	print(0, 0)
	exit()
for i in range(n - 1):
	(h, m) = get()
	curr = h * 60 + m
	if curr - prev >= 2 * s + 2:
		print(*divmod(prev + s + 1, 60))
		exit()
	else:
		prev = curr
print(*divmod(curr + s + 1, 60))


(n, s) = [int(x) for x in input().split()]
ts = []
for _ in range(n):
	(h, m) = [int(x) for x in input().split()]
	ts.append(h * 60 + m)
if ts[0] >= s + 1:
	print(0, 0)
	exit()
diffs = [y - x for (x, y) in zip(ts, ts[1:])]
for (i, diff) in enumerate(diffs):
	if diff >= 2 * s + 2:
		break
else:
	i = len(ts) - 1
t = ts[i] + s + 1
print(t // 60, t % 60)


R = lambda : map(int, input().split())
(n, s) = R()
r = 0
for _ in [0] * n:
	(h, m) = R()
	t = 60 * h + m
	if t > r + s:
		break
	r = t + s + 1
print(r // 60, r % 60)


(n, s) = map(int, input().split())
a = []
for _ in range(n):
	(h, m) = map(int, input().split())
	a.append(h * 60 + m)
if a[0] != 0 and a[0] > s:
	print(0, 0)
else:
	a.append(a[n - 1] + 2 * s + 3)
	for i in range(1, n + 1):
		if a[i] - (a[i - 1] + 2 + s) >= s:
			print((a[i - 1] + s + 1) // 60, (a[i - 1] + s + 1) % 60)
			break


import sys
sys.setrecursionlimit(2000)
from collections import Counter

def solution(n, s, planes):
	if planes[0] > s:
		print('0 0')
		return
	find = 0
	for (ind, val) in enumerate(planes):
		find = ind
		if ind != n - 1 and abs(val - planes[ind + 1]) >= 2 * s + 2:
			break
	time = planes[find] + s + 1
	h = time // 60
	m = time % 60
	print('%d %d' % (h, m))
(n, s) = [int(val) for val in input().split()]
planes = []
for i in range(n):
	(h, m) = [int(val) for val in input().split()]
	planes.append(60 * h + m)
solution(n, s, planes)


def run():
	(n, s) = [int(x) for x in input().split()]
	d = [[int(x) for x in input().split()] for _ in range(n)]
	first = d[0]
	first = first[0] * 60 + first[1]
	if first >= s + 1:
		print(0, 0)
		return
	for i in range(n - 1):
		(lft, rgt) = (d[i], d[i + 1])
		lft = lft[0] * 60 + lft[1]
		rgt = rgt[0] * 60 + rgt[1]
		if rgt - lft >= (s + 1) * 2:
			ans = lft + s + 1
			print(ans // 60, ans % 60)
			return
	rgt = d[-1]
	rgt = rgt[0] * 60 + rgt[1]
	ans = rgt + s + 1
	print(ans // 60, ans % 60)
run()


if_debag = False
input_texts = '3 17\n0 30\n1 0\n12 0\n'
input_texts = input_texts.split('\n')
it = iter(input_texts)

def my_input():
	if if_debag:
		return next(it)
	else:
		return input()
(n, s) = my_input().split()
n = int(n)
s = int(s)
hs = list()
ms = list()
for _ in range(n):
	(h, m) = my_input().split()
	h = int(h)
	m = int(m)
	hs.append(h)
	ms.append(m)
hs.append(30)
ms.append(59)
ans1 = 0
ans2 = 0
for i in range(n):
	diff = (hs[i + 1] - hs[i]) * 60 + ms[i + 1] - ms[i]
	if diff - 2 * s - 2 >= 0:
		temp = 60 * hs[i] + ms[i] + 1 + s
		ans1 = temp // 60
		ans2 = temp % 60
		break
diff = hs[0] * 60 + ms[0]
if diff - s - 1 >= 0:
	ans1 = 0
	ans2 = 0
print('{} {}'.format(ans1, ans2))


(n, s) = [int(x) for x in input().split(' ')]
last = 0
found = False
rng = 2 * s + 2
first = s + 1
f = False
ff = False
for _ in range(n):
	(h, m) = [int(x) for x in input().split(' ')]
	x = h * 60 + m
	if not f:
		if abs(x - last) >= first:
			found = True
			ff = True
		else:
			last = x
	elif not found:
		if abs(x - last) >= rng:
			found = True
		else:
			last = x
	f = True
if ff:
	print(0, 0)
else:
	res = last + s + 1
	print('{} {}'.format(res // 60, res % 60))


(n, k) = map(int, input().split())
r = 0
for _ in range(n):
	(h, m) = map(int, input().split())
	t = 60 * h + m
	if t > r + k:
		break
	r = t + k + 1
print(r // 60, r % 60)


(n, s) = map(int, input().split())
s += 1
k = []
for i in range(n):
	(a, b) = map(int, input().split())
	k += [a * 60 + b]
if k[0] >= s:
	print('0', '0')
else:
	for i in range(1, n):
		if k[i] - k[i - 1] >= 2 * s:
			print((k[i - 1] + s) // 60, (k[i - 1] + s) % 60)
			exit()
	print((k[n - 1] + s) // 60, (k[n - 1] + s) % 60)


(n, s) = map(int, input().split())
time = [0]
for i in range(n):
	(h, m) = map(int, input().split())
	time.append(h * 60 + m)
f = True
fl = True
for i in range(len(time) - 1):
	if time[1] > s:
		(ansh, ansm) = (0, 0)
		f = False
		break
	if time[i] + 2 * s + 1 < time[i + 1]:
		ansh = (time[i] + s + 1) // 60
		ansm = (time[i] + s + 1) % 60
		f = False
		break
if f:
	ansh = (time[-1] + s + 1) // 60
	ansm = (time[-1] + s + 1) % 60
print(ansh, ansm)


(n, s) = map(int, input().split())
(x, b, a) = (s * 2 + 2, 0, [])
for i in range(n):
	(h, m) = map(int, input().split())
	a.append(h * 60 + m)
if a[0] >= s + 1:
	print(0, 0)
else:
	for i in range(1, n):
		if a[i] - a[i - 1] >= x:
			b = 1
			z = a[i - 1] + s + 1
			(h, m) = (z // 60, z % 60)
			print(h, m)
			break
	if b == 0:
		z = a[n - 1] + s + 1
		print(z // 60, z % 60)


(n, s) = list(map(int, input().split()))
x = []
for i in range(n):
	zz = list(map(int, input().split()))
	zz = zz[0] * 60 + zz[-1]
	x.append(zz)
gap = 2 * s + 2
if x[0] >= s + 1:
	print('0 0')
else:
	ans = -1
	for i in range(len(x) - 1):
		diff = x[i + 1] - x[i]
		if diff >= gap:
			ans = i
			break
	ans = x[ans] + s + 1
	print(ans // 60, end=' ')
	print(ans % 60)


(n, s) = map(int, input().split())
arr = [0]
arr1 = []
for i in range(n):
	(h, m) = map(int, input().split())
	arr.append(h * 60 + m)
	arr1.append([h, m])
ans = [0, 0]
flag = 0
for i in range(1, n + 1):
	if i == 1:
		if arr[i] >= s + 1:
			k = s + 1
			ans = [0, 0]
			flag = 1
			break
	elif arr[i] - arr[i - 1] >= 2 * s + 2:
		k = arr[i - 1] + s + 1
		ans[0] = k // 60
		ans[1] = k % 60
		flag = 1
		break
if flag == 0:
	k = arr[-1] + s + 1
	ans[0] = k // 60
	ans[1] = k % 60
print(*ans)


(num_landings, min_interval) = (int(x) for x in input().split())
landings = [[int(x) for x in input().split()] for _ in range(num_landings)]
landings_in_minutes = [-min_interval - 1] + [60 * clock[0] + clock[1] for clock in landings] + [float('inf')]
for (left_landing, right_landing) in zip(landings_in_minutes[:-1], landings_in_minutes[1:]):
	if left_landing + 2 * min_interval + 2 <= right_landing:
		takeoff = left_landing + 1 + min_interval
		break
takeoff_hours = takeoff // 60
takeoff_mins = takeoff % 60
print(takeoff_hours, takeoff_mins)


def test_1():
	import math
	(n, s) = list(map(int, input().split(' ')))
	times = []
	for i in range(n):
		(h, m) = list(map(int, input().split(' ')))
		time = h * 60 + m
		times.append(time)
		if i == 0:
			if time >= s + 1:
				print(0, 0)
				break
		if i != 0:
			if time - times[i - 1] >= s * 2 + 2:
				time2 = times[i - 1] + s + 1
				print(time2 // 60, time2 % 60)
				break
		if i == n - 1:
			time2 = time + s + 1
			print(time2 // 60, time2 % 60)
test_1()


def get_nearest_takeoff():
	(landings, gap) = [int(n) for n in input().split()]
	cur_time = -(gap + 1)
	for landing in range(landings):
		(hour, minutes) = [int(n) for n in input().split()]
		time = minutes + hour * 60
		if time - cur_time >= 2 * gap + 2:
			cur_time = cur_time + gap + 1
			(hour, minutes) = (cur_time // 60, cur_time % 60)
			return str(hour) + ' ' + str(minutes)
		cur_time = time
	cur_time = cur_time + gap + 1
	(hour, minutes) = (cur_time // 60, cur_time % 60)
	return str(hour) + ' ' + str(minutes)
print(get_nearest_takeoff())


(n, s) = map(int, input().split())
x = y = 0
import sys
for i in range(n):
	(a, b) = map(int, sys.stdin.readline().split())
	z = (a - x) * 60 + (b - y)
	if x == 0 and y == 0 and (z >= s + 1) and (i == 0) and (a != 0 or b != 0):
		print(0, 0)
		exit(0)
	if z >= 2 * s + 2:
		ans = x * 60 + y + s + 1
		print(ans // 60, ans % 60)
		exit(0)
	x = a
	y = b
ans = x * 60 + y + s + 1
print(ans // 60, ans % 60)


(n, s) = list(map(int, input().split()))
a = []
ind = True
for i in range(n):
	(x, y) = list(map(int, input().split()))
	time = 60 * x + y
	if len(a) == 0 and time >= s + 1 and ind:
		print(0, 0)
		ind = False
	elif len(a) > 0 and ind:
		last = a[len(a) - 1]
		if time - last >= 2 * s + 2:
			y = last + s + 1
			print(y // 60, y % 60)
			ind = False
	a.append(time)
if ind:
	y = a[len(a) - 1] + s + 1
	print(y // 60, y % 60)


ans = 0
tem = 0
tem = int(tem)
ans = int(ans)
(n, s) = input().split()
(n, s) = [int(n), int(s)]
for x in range(0, n):
	(h, m) = input().split()
	(h, m) = [int(h), int(m)]
	tem = 60 * h + m
	if ans + s < tem:
		break
	ans = tem + s + 1
print(int(ans / 60), int(ans % 60))


(n, s) = map(int, input().split())
for i in range(n):
	if i == 0:
		(h0, m0) = map(int, input().split())
		if (h0 - 0) * 60 + m0 - 0 >= s + 1:
			early_m = 0
			early_h = 0
			break
		continue
	(h, m) = map(int, input().split())
	diff_m = (h - h0) * 60 + m - m0
	if diff_m >= (s + 1) * 2:
		early_m = (m0 + s + 1) % 60
		early_h = h0 + (m0 + s + 1) // 60
		break
	(h0, m0) = (h, m)
else:
	early_m = (m0 + s + 1) % 60
	early_h = h0 + (m0 + s + 1) // 60
print(early_h, early_m)


def preobr(s):
	(h, m) = map(int, s.split())
	return h * 60 + m
(n, s) = map(int, input().split())
(a, b) = map(int, input().split())
t = a * 60 + b
s += 1
flag = False
if t >= s:
	answer = 0
	flag = True
for i in range(n - 1):
	(a, b) = map(int, input().split())
	nextTime = a * 60 + b
	if flag:
		continue
	if nextTime - t >= 2 * s:
		answer = t + s
		flag = True
	else:
		t = nextTime
if not flag:
	answer = t + s
print(answer // 60, answer % 60)


(n, k) = [int(i) for i in input().split()]
h = [0 - k // 60]
m = [-(k % 60) - 1]
for i in range(n):
	(x, y) = input().split()
	h.append(int(x))
	m.append(int(y))
ansh = -1
ansm = -1
for i in range(1, n + 1):
	t = (h[i] - h[i - 1]) * 60 - m[i - 1] + m[i]
	if t > 2 * k + 1:
		ansh = h[i - 1]
		ansm = m[i - 1] + k + 1
		ansh += ansm // 60
		ansm = ansm % 60
		break
if ansh == -1:
	ansh = h[n]
	ansm = m[n] + k + 1
	ansh += ansm // 60
	ansm = ansm % 60
print(ansh, ansm)


(n, s) = input().split(' ')
(n, s) = (int(n), int(s))
rst = [-1, -1]
last = [0, 0]
(h, m) = input().split(' ')
(h, m) = (int(h), int(m))
if (h - last[0]) * 60 + (m - last[1]) >= s + 1:
	rst = [0, 0]
else:
	last = [h, m]
	for i in range(1, n):
		(h, m) = input().split(' ')
		(h, m) = (int(h), int(m))
		if (h - last[0]) * 60 + (m - last[1]) >= 2 * s + 2:
			rst = [last[0] + s // 60, last[1] + s % 60 + 1]
			break
		last = [h, m]
if rst == [-1, -1]:
	rst = [last[0] + s // 60, last[1] + s % 60 + 1]
if rst[1] >= 60:
	rst[0] += 1
	rst[1] -= 60
print(rst[0], rst[1])


(n, m) = map(int, input().split())
a = []
for i in range(n):
	(h, min) = map(int, input().split())
	a.append(min + h * 60)
answer = -1
if a[0] > m:
	answer = 0
else:
	for i in range(n - 1):
		if a[i + 1] - (a[i] + 1) >= m * 2 + 1:
			answer = a[i] + 1 + m
			break
if answer == -1:
	answer = a[len(a) - 1] + m + 1
print(answer // 60, answer % 60)


(n, s) = map(int, input().split())
t = []
for i in range(n):
	(a, b) = map(int, input().split())
	t.append(a * 60 + b)
if t[0] > s:
	print(0, 0)
else:
	f = 0
	for i in range(n - 1):
		if t[i + 1] - t[i] >= 2 * s + 2:
			f = 1
			break
	if f:
		print((t[i] + s + 1) // 60, (t[i] + s + 1) % 60)
	else:
		a = t[-1] + 1 + s
		print(a // 60, a % 60)


import sys, copy

def convert(h, m):
	return 60 * h + m
(n, m) = map(int, sys.stdin.readline().split())
arr = []
for _ in range(n):
	(a, b) = map(int, sys.stdin.readline().split())
	arr.append(convert(a, b))
if 1 + m <= arr[0]:
	print(0, 0)
else:
	for j in range(n - 1):
		if arr[j] + 2 + 2 * m <= arr[j + 1]:
			print((arr[j] + 1 + m) // 60, (arr[j] + 1 + m) % 60)
			sys.exit(0)
	print((arr[-1] + 1 + m) // 60, (arr[-1] + 1 + m) % 60)


(n, t) = map(int, input().split())
l = 0
(h, m) = map(int, input().split())
m += h * 60
if m - l >= t + 1:
	print(0, 0)
	exit()
l = m
for x in range(n - 1):
	(h, m) = map(int, input().split())
	m += h * 60
	if m - l >= t * 2 + 2:
		print((l + t + 1) // 60, (l + t + 1) % 60)
		exit()
	l = m
print((l + t + 1) // 60, (l + t + 1) % 60)


(n, s) = map(int, input().split())
c = 0
for i in range(n):
	(h, m) = map(int, input().split())
	a = h * 60 + m
	if a >= c + s + 1:
		exit(print(c // 60, c % 60))
	c = a + s + 1
print(c // 60, c % 60)


(n, s) = [int(i) for i in input().split()]
a = [-s - 1]
for i in range(n):
	(h, m) = [int(i) for i in input().split()]
	t = h * 60 + m
	a.append(t)
a.append(a[-1] + 2 * s + 2)
for i in range(n + 1):
	if a[i + 1] - a[i] >= 2 * s + 2:
		t = a[i] + s + 1
		print(t // 60, t % 60)
		break


import array
from bisect import *
from collections import *
import fractions
import heapq
from itertools import *
import math
import random
import re
import string
(N, S) = map(int, input().split())
hms = []
for n in range(N):
	(h, m) = map(int, input().split())
	hms.append((h, m))

def get_time(cur_h, cur_m, h, m):
	ret = (h - cur_h) * 60
	ret += m - cur_m
	return ret

def add_time(cur_h, cur_m, h, m):
	ret_h = cur_h + h
	ret_m = cur_m + m
	while ret_m >= 60:
		ret_h += 1
		ret_m -= 60
	return (ret_h, ret_m)
cur_h = 0
cur_m = 0
for n in range(N):
	(h, m) = hms[n]
	time = get_time(cur_h, cur_m, h, m)
	if n == 0 and time >= S + 1:
		print('0 0')
		break
	elif time >= S * 2 + 1:
		ans = add_time(cur_h, cur_m, 0, S)
		print(*ans)
		break
	else:
		(cur_h, cur_m) = add_time(h, m, 0, 1)
else:
	ans = add_time(cur_h, cur_m, 0, S)
	print(*ans)


MIS = lambda : map(int, input().split())
(n, s) = MIS()
time = []
for i in range(n):
	(h, m) = MIS()
	time.append(60 * h + m)
if s + 1 <= time[0]:
	print('0 0')
	exit()
for i in range(n - 1):
	takeoff = time[i] + s + 1
	if takeoff + s + 1 <= time[i + 1]:
		print(takeoff // 60, takeoff % 60)
		break
else:
	takeoff = time[-1] + s + 1
	print(takeoff // 60, takeoff % 60)


(n, s) = map(int, input().strip().split())
mi = list()
for _ in range(n):
	(h, m) = map(int, input().strip().split())
	mi.append(h * 60 + m)
res = -1
if mi[0] >= s + 1:
	res = 0
if res < 0:
	for i in range(1, n):
		d = mi[i] - mi[i - 1] - 1
		if d >= 1 + 2 * s:
			res = mi[i - 1] + s + 1
			break
	if res < 0:
		res = mi[-1] + s + 1
print(res // 60, res % 60)


(n, s) = map(int, input().split())
a = []
for i in range(n):
	(h, m) = map(int, input().split())
	a.append(h * 60 + m)
if s < a[0]:
	print('0 0')
	exit()
for i in range(n - 1):
	if a[i + 1] > a[i] + s + s + 1:
		t = a[i] + s + 1
		print(str(t // 60) + ' ' + str(t % 60))
		exit()
t = a[n - 1] + 1 + s
print(str(t // 60) + ' ' + str(t % 60))


a = input()
a = a.split(' ')
for i in range(len(a)):
	a[i] = int(a[i])
b = []
d = [0, 0]
for i in range(a[0]):
	c = input()
	c = c.split(' ')
	d[0] = int(c[0])
	d[1] = int(c[1])
	b += [d]
	d = [0, 0]
f = [0, 0]
f[0] = a[1] // 60
f[1] = a[1] % 60

def resitev():
	s = [0, 0]
	if b[0][0] * 60 + b[0][1] >= f[0] * 60 + f[1] + 1:
		print(0, 0)
		return
	for i in range(a[0] - 1):
		s[0] = b[i + 1][0] - b[i][0]
		s[1] = b[i + 1][1] - b[i][1] - 2
		if s[0] * 60 + s[1] >= 2 * (f[0] * 60 + f[1]):
			if f[1] + b[i][1] + 1 < 60:
				print(f[0] + b[i][0], f[1] + b[i][1] + 1)
				return
			else:
				print(f[0] + b[i][0] + 1, (f[1] + b[i][1] + 1) % 60)
				return
	if b[a[0] - 1][1] + f[1] + 1 < 60:
		print(b[a[0] - 1][0] + f[0], b[a[0] - 1][1] + f[1] + 1)
		return
	print(b[a[0] - 1][0] + f[0] + 1, (b[a[0] - 1][1] + f[1] + 1) % 60)
	return
resitev()


(n, s) = map(int, input().split())
(x, y) = (0, -s - 1)
s = 2 * s
flag = 0
for i in range(0, n):
	(h, m) = map(int, input().split())
	if flag == 0:
		t1 = x * 60 + y + 1
		t2 = h * 60 + m
		t3 = t2 - t1
		if t3 > s:
			print((t1 + s // 2) // 60, (t1 + s // 2) % 60)
			flag = 1
		else:
			y = m
			x = h
			continue
if flag == 0:
	t1 = x * 60 + y + 1 + s // 2
	print(t1 // 60, t1 % 60)


(n, s) = map(int, input().split())
(temp, ans) = (0, False)
for _ in range(n):
	(h, m) = map(int, input().split())
	if not ans:
		diff = h * 60 + m - (temp + 1)
		if diff >= s:
			ans = True
		else:
			temp = h * 60 + m + s + 1
print(temp // 60, temp % 60)


(array, atime) = ([], [])
(n, s) = map(int, input().split())
for i in range(n):
	(h, m) = map(int, input().split())
	array.append(h * 60 + m)
for i in range(len(array)):
	if i + 1 != len(array):
		atime.append((array[i + 1] - array[i] - 2) / 2)
	else:
		atime.append((1439 - array[i] - 2) / 2)
bool = False
for i in range(len(atime)):
	if array[0] - 1 >= s:
		bool = True
		answer = 0
		break
	if atime[i] >= s:
		bool = True
		answer = array[i] + 1 + s
		break
if bool is True:
	print(answer // 60, answer % 60)
else:
	print((array[len(atime) - 1] + 1 + s) // 60, (array[len(atime) - 1] + 1 + s) % 60)


(n, s) = map(int, input().split())
(h1, m1) = map(int, input().split())
if h1 * 60 + m1 >= s + 1:
	print('\n0 0')
	raise SystemExit()
n -= 1
while n > 0:
	(h2, m2) = map(int, input().split())
	if (h2 - h1) * 60 + (m2 - m1) >= 2 * s + 2:
		print('\n', h1 + (m1 + s + 1) // 60, ' ', (m1 + s + 1) % 60)
		break
	h1 = h2
	m1 = m2
	n -= 1
if n == 0:
	print('\n', h1 + (m1 + s + 1) // 60, ' ', (m1 + s + 1) % 60)


rd = lambda : map(int, input().split())
(n, s) = rd()
a = [-s - 1]
for _ in range(n):
	(h, m) = rd()
	a.append(h * 60 + m)
x = 0
for i in range(1, len(a)):
	if a[i] - a[i - 1] > 2 * s + 1:
		break
	x = a[i] + s + 1
print(*divmod(x, 60))


(n, s) = map(int, input().split())
(a, b) = map(int, input().split())
if a * 60 + b >= s + 1:
	print(0, 0)
else:
	X = 0
	for i in range(n - 1):
		(c, d) = map(int, input().split())
		if c * 60 + d - (a * 60 + b) >= 2 * s + 2:
			M = a * 60 + b + 1 + s
			print(M // 60, M % 60)
			X = 1
			break
		else:
			(a, b) = (c, d)
	if X == 0:
		M = a * 60 + b + 1 + s
		print(M // 60, M % 60)


(n, s) = map(int, input().split())
t = [0 for i in range(n)]
for i in range(n):
	(_, __) = map(int, input().split())
	t[i] = _ * 60 + __
if t[0] >= s + 1:
	print('0 0')
	exit()
else:
	for i in range(n - 1):
		td = t[i + 1] - t[i]
		if td >= 2 * (s + 1):
			print((t[i] + s + 1) // 60, end=' ')
			print((t[i] + s + 1) % 60)
			exit()
	print((t[n - 1] + s + 1) // 60, end=' ')
	print((t[n - 1] + s + 1) % 60)
	exit()


def next(h1, m1, minutes):
	return (h1 + (m1 + minutes) // 60, (m1 + minutes) % 60)
(n, s) = map(int, input().split())
schedule = []
for i in range(n):
	schedule.append(tuple(map(int, input().split())))
if schedule[0][0] * 60 + schedule[0][1] >= s + 1:
	print('0 0')
else:
	for i in range(1, len(schedule)):
		if (schedule[i][0] - schedule[i - 1][0]) * 60 + (schedule[i][1] - schedule[i - 1][1]) >= 2 * s + 2:
			res = next(schedule[i - 1][0], schedule[i - 1][1], s + 1)
			print(str(res[0]) + ' ' + str(res[1]))
			break
	else:
		res = next(schedule[i][0], schedule[i][1], s + 1)
		print(str(res[0]) + ' ' + str(res[1]))


(n, s) = [int(i) for i in input().split()]
i = 0
a = []
bool = True
while i < n:
	(h, m) = [int(i) for i in input().split()]
	time = 60 * h + m
	if len(a) == 0 and time >= s + 1 and bool:
		print(0, 0)
		bool = False
	elif len(a) > 0 and bool:
		last = a[len(a) - 1]
		if time - last >= 2 * s + 2:
			m = last + s + 1
			print(m // 60, m % 60)
			bool = False
	a.append(time)
	i += 1
if bool:
	m = a[len(a) - 1] + s + 1
	print(m // 60, m % 60)


(n, s) = map(int, input().split())
t = []
for _ in range(n):
	x = input().split()
	t.append(int(x[0]) * 60 + int(x[1]))
if t[0] >= s + 1:
	print(0, 0)
else:
	for i in range(n - 1):
		if t[i] + 2 * (s + 1) <= t[i + 1]:
			print((t[i] + s + 1) // 60, (t[i] + s + 1) % 60)
			break
	else:
		print((t[-1] + s + 1) // 60, (t[-1] + s + 1) % 60)


ok = [True for _ in range(26 * 60 + 1)]
(n, s) = map(int, input().split())
for _ in range(n):
	(h, m) = map(int, input().split())
	for t in range(max(0, h * 60 + m - s), h * 60 + m + s + 1):
		ok[t] = False
for t in range(len(ok)):
	if ok[t]:
		print('%d %d' % (t // 60, t % 60))
		break


(n, s) = map(int, input().strip().split(' '))
lst = []
for i in range(n):
	(h, m) = map(int, input().strip().split(' '))
	p = h * 60 + m
	lst.append(p)
if lst[0] >= s + 1:
	print('0 0')
else:
	dif = []
	f = 0
	for i in range(1, n):
		dif.append(lst[i] - lst[i - 1])
	for i in range(len(dif)):
		if dif[i] > 2 * s + 1:
			k = lst[i] + s + 1
			h = k // 60
			m = k % 60
			print(h, m, end=' ')
			f = 1
			break
	if f == 0:
		k = lst[n - 1] + 1 + s
		h = k // 60
		m = k % 60
		print(h, m, end=' ')


(n, s) = map(int, input().split())
chegadas = []
for _ in range(n):
	(h, m) = map(int, input().split())
	chegadas.append(h * 60 + m)
pos = -1
if chegadas[0] > s:
	print(0, 0)
else:
	for i in range(n - 1):
		if chegadas[i + 1] - chegadas[i] > s * 2 + 1:
			pos = i
			break
	t = chegadas[pos] + s + 1
	(h, m) = (int(t / 60), t % 60)
	print(h, m)


(n, s) = (int(x) for x in input().split())
(h, m) = ([], [])
for i in range(n):
	(h1, m1) = (int(x) for x in input().split())
	h.append(h1)
	m.append(m1)
flag = True
if h[0] * 60 + m[0] - s >= 1:
	flag = False
	print(0, 0)
else:
	for i in range(n - 1):
		z = h[i] * 60 + m[i] + s + 1
		if z <= h[i + 1] * 60 + m[i + 1] - s - 1:
			print(z // 60, z - z // 60 * 60)
			flag = False
			break
if flag:
	z = h[n - 1] * 60 + m[n - 1] + s + 1
	print(z // 60, z - z // 60 * 60)


(n, s) = [int(x) for x in input().split()]
(d, k) = (0, 0)
for i in range(n):
	(h, m) = [int(x) for x in input().split()]
	raz = h * 60 + m - (d * 60 + k)
	if i == 0 and raz >= s + 1:
		print(d, k)
		break
	elif raz >= s * 2 + 2:
		mojno = d * 60 + k + s + 1
		l = mojno // 60
		a = mojno % 60
		print(l, a)
		break
	else:
		d = h
		k = m
else:
	mojno = d * 60 + k + s + 1
	l = mojno // 60
	a = mojno % 60
	print(l, a)


import sys
import math
import bisect

def solve(A, k):
	n = len(A)
	if len(A) == 0:
		return (0, 0)
	if A[0] - 0 - 1 >= k:
		return (0, 0)
	for i in range(1, n):
		if A[i] - A[i - 1] - 1 >= 2 * k + 1:
			val = A[i - 1] + k + 1
			return (val // 60, val % 60)
	val = A[-1] + k + 1
	return (val // 60, val % 60)

def main():
	(n, k) = map(int, input().split())
	A = []
	for i in range(n):
		(h, m) = map(int, input().split())
		A.append(h * 60 + m)
	(h, m) = solve(A, k)
	print('%d %d' % (h, m))
main()


def process(data, n, delay_time):
	time = []
	for i in range(n):
		time.append(60 * data[i][0] + data[i][1])
	if time[0] >= delay_time + 1:
		return 0
	else:
		for i in range(1, n):
			if time[i] - time[i - 1] >= 2 * delay_time + 2:
				return time[i - 1] + delay_time + 1
	return time[-1] + delay_time + 1
sentinel = ''
lines = []
line1 = list(map(int, input().split()))
n = line1[0]
delay_time = line1[1]
data = [[0, 0] for cols in range(n)]
for i in range(n):
	temp = list(map(int, input().split()))
	data[i][0] = temp[0]
	data[i][1] = temp[1]
	i += 1
land_time = process(data, n, delay_time)
print(land_time // 60, land_time % 60)


inp = input().split()
n = int(inp[0])
s = int(inp[1])
gap = 2 * s + 1
inp1 = input().split()
first = int(inp1[0]) * 60 + int(inp1[1])
if first >= s + 1:
	print('0 0')
	exit()
prev = first
for each in range(n - 1):
	new_raw = input().split()
	new = int(new_raw[0]) * 60 + int(new_raw[1])
	if new - (prev + 1) >= gap:
		print(str((prev + 1 + s) // 60) + ' ' + str((prev + 1 + s) % 60))
		exit()
	prev = new
print(str((prev + 1 + s) // 60) + ' ' + str((prev + 1 + s) % 60))


(n, s) = map(int, input().split())
l = []
cond = True
for i in range(n):
	(x, y) = map(int, input().split())
	l.append(x * 60 + y)
if l[0] > s:
	print('0 0')
	cond = False
else:
	for i in range(n - 1):
		if l[i + 1] - l[i] >= 2 * s + 2:
			cond = False
			print((l[i] + s + 1) // 60, ' ', (l[i] + s + 1) % 60, sep='')
			break
if cond:
	print((l[n - 1] + s + 1) // 60, ' ', (l[n - 1] + s + 1) % 60, sep='')


import os
import sys
debug = True
if debug and os.path.exists('input.in'):
	input = open('input.in', 'r').readline
else:
	debug = False
	input = sys.stdin.readline

def inp():
	return int(input())

def inlt():
	return list(map(int, input().split()))

def insr():
	s = input()
	return s[:len(s) - 1]

def invr():
	return map(int, input().split())
test_count = 1
if debug:
	test_count = inp()
for t in range(test_count):
	if debug:
		print('Test Case #', t + 1)
	(n, s) = invr()
	a = [0] * n
	for i in range(n):
		(h, m) = invr()
		a[i] = h * 60 + m
	last = 0
	ans = -1
	for i in range(0, n):
		curr = a[i]
		if i == 0:
			if curr >= s + 1:
				ans = 0
				break
		if curr - last >= 2 * s + 2:
			ans = last + s + 1
			break
		last = curr
	if ans == -1:
		ans = last + s + 1
	print(ans // 60, ans % 60)


import sys
(n, s) = [int(x) for x in input().split()]
(h1, m1) = [int(x) for x in input().split()]
if h1 * 60 + m1 >= s + 1:
	print(0, 0)
	sys.exit()
for i in range(n - 1):
	(h2, m2) = [int(x) for x in input().split()]
	if h2 * 60 + m2 >= h1 * 60 + m1 + 2 + s * 2:
		z = h1 * 60 + m1 + 1 + s
		print(z // 60, z % 60)
		sys.exit()
	h1 = h2
	m1 = m2
z = h1 * 60 + m1 + 1 + s
print(z // 60, z % 60)


def preobr(s):
	(h, m) = list(map(int, s.split()))
	return h * 60 + m
(n, s) = list(map(int, input().split()))
t = preobr(input())
s += 1
fl = False
if t >= s:
	res = 0
	fl = True
for i in range(n - 1):
	new_t = preobr(input())
	if fl:
		continue
	if new_t - t >= 2 * s:
		res = t + s
		fl = True
	else:
		t = new_t
if not fl:
	res = t + s
print(res // 60, res % 60)


(n, s) = map(int, input().split())
arr = []
b = 0
for _ in range(n):
	(h, m) = map(int, input().split())
	arr.append(h * 60 + m)
if arr[0] > s:
	print('0 0')
else:
	for i in range(1, n):
		if arr[i] - arr[i - 1] - 1 > 2 * s:
			b = 1
			print((arr[i - 1] + 1 + s) // 60, (arr[i - 1] + 1 + s) % 60)
			break
	if b == 0:
		print((arr[n - 1] + 1 + s) // 60, (arr[n - 1] + 1 + s) % 60)


f = lambda h, m: h * 60 + m
g = lambda t: f'{t // 60} {t % 60}'
(n, s) = map(int, input().split())
l = [f(*map(int, input().split())) for _ in range(n)] + [float('inf')]
l.sort()
if l[0] != 0 and l[0] >= s + 1:
	print(0, 0)
	exit()
for i in range(n):
	if l[i + 1] - l[i] >= 2 * s + 2:
		print(g(l[i] + s + 1))
		break


a = True
(n, s) = [int(x) for x in input().split()]
(ph, pm) = [int(x) for x in input().split()]
if ph * 60 + pm >= s + 1:
	print(0, 0)
	a = False
	return
for x in range(n - 1):
	(h, m) = [int(x) for x in input().split()]
	if h * 60 + m >= ph * 60 + pm + 2 + 2 * s:
		print((ph * 60 + pm + s + 1) // 60, (ph * 60 + pm + s + 1) % 60)
		a = False
		break
	else:
		ph = h
		pm = m
if a:
	print((ph * 60 + pm + s + 1) // 60, (ph * 60 + pm + s + 1) % 60)


def readline():
	return tuple(map(int, input().split()))

def readlines(count):
	while count:
		count -= 1
		yield readline()

def main():
	(n, s) = readline()
	shedule = [h * 60 + m for (h, m) in readlines(n)]
	shedule.append(float('inf'))
	prev = -s
	for time in shedule:
		if prev + s < time - s:
			print(*divmod(prev + s, 60))
			break
		prev = time + 1
main()


(n, s) = map(int, input().split())
(h, m) = map(int, input().split())
t = m + h * 60
if t - 1 >= s:
	print(0, 0)
	exit()
ok = True
for i in range(n - 1):
	(h, m) = map(int, input().split())
	t1 = m + h * 60
	if t1 - t - 2 >= s * 2:
		p = t + s + 1
		pm = p % 60
		ph = p // 60
		print(ph, pm)
		ok = False
		break
	t = t1
if ok:
	p = t + s + 1
	pm = p % 60
	ph = p // 60
	print(ph, pm)


def timediff(ch, cm, h, m):
	if ch == h:
		numMin = m - cm
	else:
		numMin = 60 - cm + m
		numMin += 60 * (h - ch - 1)
	return numMin
line = input().split(' ')
n = int(line[0])
s = int(line[1])
timespacing = []
for i in range(n):
	line = input().split(' ')
	temph = int(line[0])
	tempm = int(line[1])
	timespacing.append(timediff(0, 0, temph, tempm))
bestH = -1
bestM = -1
if timespacing[0] >= s + 1:
	bestH = 0
	bestM = 0
if bestH == -1 and bestM == -1:
	for i in range(n):
		if i < n - 1:
			timedifference = timespacing[i + 1] - timespacing[i]
		else:
			break
		if timedifference >= 2 * s + 2:
			timedone = timespacing[i] + s + 1
			bestH = int(timedone / 60)
			bestM = timedone % 60
			break
if bestH == -1 and bestM == -1:
	timedone = timespacing[-1] + s + 1
	bestH = int(timedone / 60)
	bestM = timedone % 60
print(str(bestH) + ' ' + str(bestM))


import sys
(n, s) = map(int, input().split())
inputs = [sys.stdin.readline() for _ in range(n)]
init = 0
checked = False
last = False
res = [0, 0]
for i in inputs:
	(h, m) = map(int, i.split())
	current = h * 60 + m
	if not checked and init == 0 and (init + s + 1 <= current):
		break
	if init + 2 * s + 2 <= current:
		a = (init + s + 1) // 60
		b = (init + s + 1) % 60
		res = [a, b]
		break
	else:
		init = current
		if init == 0:
			checked = True
	if i == inputs[-1]:
		last = True
if last:
	(h, m) = map(int, inputs[-1].split())
	minutes = h * 60 + m + s + 1
	a = minutes // 60
	b = minutes % 60
	res = [a, b]
print(str(res[0]) + ' ' + str(res[1]))


(n, k) = map(int, input().split())
hr = []
mn = []
s = k * 2 + 2
for i in range(0, n):
	(x, y) = map(int, input().split())
	hr.insert(i, x)
	mn.insert(i, y)
fst = hr[0] * 60 + mn[0]
if fst >= k + 1:
	print('0 0')
else:
	v = 0
	lst = int(0)
	for j in range(0, n - 1):
		fst = hr[j] * 60 + mn[j]
		lst = hr[j + 1] * 60 + mn[j + 1]
		df = lst - fst
		if df >= s:
			v = 1
			break
	if v == 1:
		fst += 1 + k
		print(fst // 60, ' ', fst % 60)
	else:
		lst = hr[n - 1] * 60 + mn[n - 1] + 1 + k
		print(lst // 60, ' ', lst % 60)


(n, s) = (int(i) for i in input().split())
times = [0]
for i in range(n):
	(h, m) = (int(j) for j in input().split())
	time = h * 60 + m
	times.append(time)
requiredTime = -1
if times[1] >= s + 1:
	requiredTime = times[0]
else:
	for i in range(1, n):
		if times[i + 1] - times[i] > s + s + 1:
			requiredTime = times[i] + 1 + s
			break
if requiredTime == -1:
	requiredTime = times[n] + 1 + s
h = requiredTime // 60
m = requiredTime % 60
print(h, m)


R = lambda : map(int, input().split())
(n, s) = R()
t = [-s - 1] + [60 * h + m for (h, m) in (R() for _ in [0] * n)] + [1600]
x = t[next((i for i in range(n + 1) if t[i + 1] > t[i] + 2 * s + 1))] + s + 1
print(x // 60, x % 60)


def main():
	[n, s] = [int(i) for i in input().split()]
	times = []
	for i in range(n):
		[hour, minute] = [int(i) for i in input().split()]
		times.append(hour * 60 + minute)
	ans = earliest(times, s)
	hour = ans // 60
	minute = ans % 60
	print(hour, minute)

def earliest(times, s):
	n = len(times)
	if times[0] >= s + 1:
		return 0
	for i in range(n - 1):
		bef = times[i]
		aft = times[i + 1]
		if aft - bef >= 2 * s + 2:
			return bef + s + 1
	return times[n - 1] + s + 1

def __starting_point():
	main()
__starting_point()


(n, s) = map(int, input().split(' '))
last = 0
sol = None
for i in range(n):
	(h, m) = map(int, input().split(' '))
	curr = h * 60 + m + 1
	if curr - last >= s + 2:
		sol = last
	else:
		last = curr + s
if sol:
	print(sol // 60, sol % 60)
else:
	print(last // 60, last % 60)


(n, s) = map(int, input().split())

def diff(t1, t2):
	if t2[0] == t1[0]:
		return t2[1] - t1[1]
	else:
		return t2[1] + 60 - t1[1] + (t2[0] - t1[0] - 1) * 60

def add(t, s):
	h = t[0]
	m = t[1]
	h += s // 60
	s %= 60
	m += s
	if m > 59:
		m %= 60
		h += 1
	return (h, m)
a = [(0, 0)]
for i in range(n):
	(h, m) = map(int, input().split())
	a.append((h, m))
if diff(a[0], a[1]) > s:
	print(0, 0)
	exit()
for i in range(1, n + 1):
	if diff(a[i - 1], a[i]) > 2 * s + 1:
		ans = add(a[i - 1], s + 1)
		print(ans[0], ans[1])
		exit()
ans = add(a[i], s + 1)
print(ans[0], ans[1])


(n, s) = list(map(int, input().strip().split()))
schd = []
for i in range(n):
	(h, m) = list(map(int, input().strip().split()))
	schd.append([h, m])
(h, m) = schd[0]
take_off_m = (s + 1) % 60
take_off_h = (s + 1) // 60
if take_off_h < h or (take_off_h == h and take_off_m <= m):
	print(0, 0)
else:
	find = False
	for i in range(n - 1):
		(h, m) = schd[i]
		take_off_m = (m + 2 * s + 2) % 60
		take_off_h = h + (m + 2 * s + 2) // 60
		(next_h, next_m) = schd[i + 1]
		if take_off_h < next_h or (take_off_h == next_h and take_off_m <= next_m):
			take_off_m = (m + s + 1) % 60
			take_off_h = h + (m + s + 1) // 60
			print(take_off_h, take_off_m)
			find = True
			break
	if not find:
		(h, m) = schd[-1]
		take_off_m = (m + s + 1) % 60
		take_off_h = h + (m + s + 1) // 60
		print(take_off_h, take_off_m)


from sys import stdin
max_val = int(10000000000000.0)
min_val = int(-10000000000000.0)

def read_int():
	return int(stdin.readline())

def read_ints():
	return [int(x) for x in stdin.readline().split()]

def read_str():
	return input()

def read_strs():
	return [x for x in stdin.readline().split()]
(nb_landings, interval) = read_ints()
prev_landing = 0
for _ in range(nb_landings):
	(hour, minute) = read_ints()
	last_landing = hour * 60 + minute
	if last_landing - prev_landing <= interval:
		prev_landing = last_landing + interval + 1
	else:
		break
print(prev_landing // 60, prev_landing % 60)


def to_minutes(h, m):
	return h * 60 + m

def to_retard(m):
	return divmod(m, 60)
(n, s) = [int(i) for i in input().split(' ')]
takeoffs = []
for i in range(n):
	takeoff_time = to_minutes(*[int(i) for i in input().split(' ')])
	takeoffs.append(takeoff_time)

def solve():
	if takeoffs[0] > s:
		return 0
	for i in range(n - 1):
		if takeoffs[i + 1] - takeoffs[i] - 1 >= 2 * s + 1:
			return takeoffs[i] + s + 1
	return takeoffs[-1] + s + 1
print(*to_retard(solve()))


(n, s) = map(int, input().split())
v = [tuple(map(int, input().split())) for _ in range(n)]
v.sort()
cur = 0
for (a, b) in v:
	t = a * 60 + b
	if t >= cur + 1 + s:
		print(cur // 60, cur % 60)
		break
	cur = t + 1 + s
if cur > v[-1][0] * 60 + v[-1][1]:
	print(cur // 60, cur % 60)


(n, s) = map(int, input().split())
lx = 0
(h, m) = map(int, input().split())
nx = h * 60 + m
if nx - s - 1 >= 0:
	print(0, 0)
else:
	lx = nx + 1
	for i in range(1, n):
		(h, m) = map(int, input().split())
		nx = h * 60 + m
		if lx + 2 * s < nx:
			print((lx + s) // 60, (lx + s) % 60)
			break
		lx = nx + 1
	else:
		print((lx + s) // 60, (lx + s) % 60)


(n, s) = map(int, input().split())
t = [[0, 0]]
for j in range(n):
	(a, b) = map(int, input().split())
	total = a * 60 + b
	last = t[-1][0] * 60 + t[-1][1] + 1
	t.append([a, b])
	if j == 0:
		if total >= s + 1:
			print(0, 0)
			break
	if total - last > 2 * s:
		u = last + s
		print(u // 60, u % 60)
		break
	if j == n - 1:
		x = t[-1][0] * 60 + t[-1][1]
		print((x + s + 1) // 60, (x + s + 1) % 60)
		break


(n, k) = map(int, input().split())
(h, m) = map(int, input().split())
if 60 * h + m >= k + 1:
	print(0, 0)
else:
	(prev_hours, prev_minutes) = (h, m + 1)
	flag = 0
	for _ in range(n - 1):
		(h, m) = map(int, input().split())
		if 60 * prev_hours + prev_minutes + k + 1 + k <= 60 * h + m:
			prev_minutes += k
			print(prev_hours + prev_minutes // 60, prev_minutes % 60)
			flag = 1
			break
		else:
			(prev_hours, prev_minutes) = (h, m + 1)
	if flag == 0:
		prev_minutes += k
		print(prev_hours + prev_minutes // 60, prev_minutes % 60)


li = input().split(' ')
n = int(li[0])
s = int(li[1])
time = []
for i in range(0, n):
	in_tmp = input().split(' ')
	h = int(in_tmp[0])
	m = int(in_tmp[1])
	time.append(h * 60 + m)
t = 0
if time[0] != 0:
	if 1 + s <= time[0]:
		print(0, 0)
		exit()
for i in range(0, n - 1):
	if time[i] + 1 + s + 1 + s <= time[i + 1]:
		t = time[i] + 1 + s
		print(t // 60, t % 60)
		exit()
t = time[n - 1] + 1 + s
print(t // 60, t % 60)


(n, s) = list(map(int, input().split(' ')))
v0 = 0
fl = False
for i in range(n):
	(h, m) = list(map(int, input().split(' ')))
	v = h * 60 + m
	if i == 0 and v - 1 >= s:
		m2 = 0
		h2 = 0
		fl = True
		break
	elif (v - v0 - 2) // 2 >= s:
		v2 = v0 + s + 1
		m2 = v2 % 60
		h2 = v2 // 60
		fl = True
		break
	else:
		v0 = v
if fl:
	print(h2, m2)
else:
	m2 = (v0 + s + 1) % 60
	h2 = (v0 + s + 1) // 60
	print(h2, m2)


from sys import stdin, stdout
from math import factorial
from math import log10
(n, s) = map(int, stdin.readline().split())
challengers = []
for i in range(n):
	(h, m) = map(int, stdin.readline().split())
	challengers.append(h * 60 + m)
challengers.sort()
ans = 0
for h in range(30):
	if ans:
		break
	for m in range(60):
		t = h * 60 + m
		for j in range(n):
			if abs(challengers[j] - t) <= s:
				break
		else:
			ans = (h, m)
			break
stdout.write(str(ans[0]) + ' ' + str(ans[1]))


def righttime(n, s, dep):
	if dep[0] >= s + 1:
		return '0 0'
	for i in range(n - 1):
		if dep[i] + 2 * s + 2 <= dep[i + 1]:
			m = dep[i] + s + 1
			return '{} {}'.format(m // 60, m % 60)
	m = dep[n - 1] + s + 1
	return '{} {}'.format(m // 60, m % 60)
(n, s) = map(int, input().split())
departures = []
for _ in range(n):
	(a, b) = map(int, input().split())
	departures.append(60 * a + b)
print(righttime(n, s, departures))


(n, s) = [int(w) for w in input().split()]
r = 0
for _ in range(n):
	(h, m) = [int(w) for w in input().split()]
	t = 60 * h + m
	if t > r + s:
		break
	r = t + s + 1
print(r // 60, r % 60)


import math
(n, s) = [int(x) for x in input().split()]
arr = list()
for i in range(0, n):
	(x, y) = [int(x) for x in input().split()]
	arr.append(60 * x + y)
ans = arr[n - 1] + s + 1
if arr[0] <= s:
	for i in range(1, n):
		if arr[i] - (arr[i - 1] + 1 + s) > s:
			ans = arr[i - 1] + s + 1
			break
else:
	ans = 0
print(int(ans / 60), ans % 60)


from itertools import *
(n, s) = map(int, input().split())
times = []
for i in range(n):
	(h, m) = map(int, input().split())
	times.append(h * 60 + m)
times.sort()
for t in count():
	if all((abs(u - t) > s for u in times)):
		print(*divmod(t, 60))
		break


I = lambda : map(int, input().split())
(n, s) = I()
l = [0]
for i in range(n):
	(q, w) = I()
	q = q * 60 + w
	l += [q]
if l[1] - l[0] > s:
	exit(print(0, 0))
for i in range(n):
	if l[i + 1] - l[i] > 2 * s + 1:
		l[i] += s + 1
		exit(print(l[i] // 60, l[i] % 60))
l[-1] += s + 1
print(l[-1] // 60, l[-1] % 60)


(n, s) = map(int, input().split())
q = 0
for i in range(n):
	(a, b) = map(int, input().split())
	p = a * 60 + b
	if i == 0 and p > s:
		rec = 0
		break
	if p - q > s * 2 + 1:
		rec = q + s + 1
		break
	q = p
	rec = q + s + 1
(h, m) = (rec // 60, rec % 60)
print(h, m)


(n, s) = map(int, input().split())
l = [0]
da = []
for i in range(n):
	(h, m) = map(int, input().split())
	l.append(h * 60 + m)
for i in range(n):
	da.append(l[i + 1] - l[i])
if da[0] > s:
	print(0, 0)
	exit(0)
ans = 0
for i in range(len(da)):
	if da[i] > 2 * s + 1:
		ans = l[i] + s + 1
		break
if ans != 0:
	print(ans // 60, ans % 60)
else:
	ans = l[n] + s + 1
	print(ans // 60, ans % 60)


(n, s) = map(int, input().split())
a = []
for i in range(n):
	(h, m) = map(int, input().split())
	a.append(h * 60 + m)
if a[0] > s:
	print('0 0')
	exit(0)
ans = -1
for i in range(1, n):
	if a[i] - a[i - 1] - 2 >= 2 * s:
		ans = a[i - 1] + s + 1
		break
if ans == -1:
	ans = a[n - 1] + s + 1
print(ans // 60, end=' ')
print(ans % 60)


(n, s) = map(int, input().split(' '))
l = list()
for i in range(n):
	(a, b) = map(int, input().split(' '))
	l.append(a * 60 + b)
if l[0] - s >= 1:
	print(0, 0)
else:
	for i in range(n - 1):
		t = l[i + 1] - l[i] - 1 - 2 * s
		if t >= 1:
			tt = l[i] + 1 + s
			(h, m) = (tt // 60, tt % 60)
			print(h, m)
			break
	else:
		tt = l[-1] + 1 + s
		(h, m) = (tt // 60, tt % 60)
		print(h, m)


(n, k) = map(int, input().split())
cur = 0
flag = 0
ans = 0

def pr(x):
	print(x // 60, x % 60)
for i in range(n):
	(x, y) = map(int, input().split())
	tmp = x * 60 + y
	if i == 0:
		if tmp - cur >= k + 1:
			flag = 1
			ans = 0
	if flag == 1:
		continue
	if tmp - cur >= k * 2 + 2:
		flag = 1
		ans = cur + k + 1
	cur = tmp
if flag == 1:
	pr(ans)
else:
	pr(cur + k + 1)


(n, s) = map(int, input().split())
h = list()
m = list()
l = list()
l.append(0)
for _ in range(n):
	(x, y) = map(int, input().split())
	h.append(x)
	m.append(y)
	l.append(x * 60 + y)
if l[1] != 0 and l[1] - l[0] >= s + 1:
	print(0, 0)
else:
	k = 2 * s + 2
	r = 0
	for i in range(n):
		if l[i + 1] - l[i] >= k:
			r = l[i] + s + 1
			break
		else:
			continue
	if r == 0:
		r = l[n] + s + 1
	print(r // 60, r % 60)


(n, m) = map(int, input().split())
b = []
d = []
for x in range(n):
	if x == 0:
		(a, c) = map(int, input().split())
		if a * 60 + c > m:
			b.append('0 0')
		d.append(a * 60 + c)
	else:
		(a, c) = map(int, input().split())
		if a * 60 + c - d[-1] > m * 2 + 1:
			f = d[-1] + m + 1
			b.append(str(f // 60) + ' ' + str(f % 60))
		d.append(a * 60 + c)
if len(b) == 0:
	f = d[-1] + m + 1
	b.append(str(f // 60) + ' ' + str(f % 60))
print(b[0])


I = lambda : map(int, input().split())
(n, s) = I()
T = []
for _ in range(n):
	(h, m) = I()
	T.append(60 * h + m)
if T[0] > s:
	print(0, 0)
else:
	for i in range(n - 1):
		if T[i + 1] - T[i] > 2 * s + 1:
			print(*divmod(T[i] + s + 1, 60))
			break
	else:
		print(*divmod(T[-1] + s + 1, 60))


(n, s) = map(int, input().split())
timetable = [0, 0]
for i in range(0, n):
	(h, m) = map(int, input().split())
	timetable.append(h)
	timetable.append(m)
for i in range(0, n):
	gap = timetable[2 * (i + 1)] * 60 + timetable[2 * (i + 1) + 1] - timetable[2 * i] * 60 - timetable[2 * i + 1]
	if gap >= 2 * s + 2:
		ans = timetable[2 * i] * 60 + timetable[2 * i + 1] + s + 1
		break
	else:
		ans = timetable[2 * (i + 1)] * 60 + timetable[2 * (i + 1) + 1] + s + 1
if timetable[2] * 60 + timetable[3] >= s + 1:
	ans = 0
h = ans // 60
m = ans % 60
print(h, m)


import math
A = list(map(int, input().split()))
n = A[0]
s = A[1]
L = []
for i in range(0, n):
	L.append(list(map(int, input().split())))
h = L[n - 1][0]
m = L[n - 1][1] + s + 1
if n > 1:
	for i in range(0, n - 1):
		if (L[i + 1][0] - L[i][0]) * 60 + (L[i + 1][1] - L[i][1]) >= 2 * s + 2:
			h = L[i][0]
			m = L[i][1] + s + 1
			break
if L[0][0] * 60 + L[0][1] >= s + 1:
	h = 0
	m = 0
if m >= 60:
	h += math.floor(m / 60)
	m = m % 60
print(h, m)


(n, s) = map(int, input().split(' '))
lst = []
for i in range(n):
	(h, m) = map(int, input().split(' '))
	lst.append(h * 60 + m)
	if i == 0:
		if lst[-1] >= s + 1:
			print(0, 0)
			exit()
	if lst[i] - lst[i - 1] >= 2 * s + 2:
		print((lst[i - 1] + s + 1) // 60, (lst[i - 1] + s + 1) % 60)
		exit()
print((lst[-1] + s + 1) // 60, (lst[-1] + s + 1) % 60)


from __future__ import division, print_function
import os, sys
from io import BytesIO, IOBase
if sys.version_info[0] < 3:
	from __builtin__ import xrange as range
	from future_builtins import ascii, filter, hex, map, oct, zip

def ii():
	return int(input())

def si():
	return input()

def mi():
	return map(int, input().strip().split(' '))

def msi():
	return map(str, input().strip().split(' '))

def li():
	return list(mi())

def dmain():
	sys.setrecursionlimit(1000000)
	threading.stack_size(1024000)
	thread = threading.Thread(target=main)
	thread.start()
import sys
input = sys.stdin.readline
scanner = lambda : int(input())
string = lambda : input().rstrip()
get_list = lambda : list(read())
read = lambda : map(int, input().split())
get_float = lambda : map(float, input().split())

def ceil(x):
	if x != int(x):
		x = int(x) + 1
	return x

def factorial(x, m):
	val = 1
	while x > 0:
		val = val * x % m
		x -= 1
	return val

def fact(x):
	val = 1
	while x > 0:
		val *= x
		x -= 1
	return val

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return a * b // math.gcd(a, b)

def is_integer(n):
	return math.ceil(n) == math.floor(n)

def nCr(n, k):
	if k > n:
		return 0
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def primefs(n):
	primes = {}
	while n % 2 == 0 and n > 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0 and n > 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	(prime[0], prime[1]) = (False, False)
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime

def phi(n):
	result = n
	p = 2
	while p * p <= n:
		if n % p == 0:
			while n % p == 0:
				n = n // p
			result = result * (1.0 - 1.0 / float(p))
		p = p + 1
	if n > 1:
		result = result * (1.0 - 1.0 / float(n))
	return int(result)

def is_prime(n):
	if n == 0:
		return False
	if n == 1:
		return True
	for i in range(2, int(n ** (1 / 2)) + 1):
		if not n % i:
			return False
	return True

def next_prime(n, primes):
	while primes[n] != True:
		n += 1
	return n
MAXN = int(100000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i
spf = [0 for i in range(MAXN)]

def factoriazation(x):
	res = []
	for i in range(2, int(x ** 0.5) + 1):
		while x % i == 0:
			res.append(i)
			x //= i
	if x != 1:
		res.append(x)
	return res

def factors(n):
	res = []
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			res.append(i)
			res.append(n // i)
	return list(set(res))

def int_array():
	return list(map(int, input().strip().split()))

def float_array():
	return list(map(float, input().strip().split()))

def str_array():
	return input().strip().split()

def binary_search(low, high, w, h, n):
	while low < high:
		mid = low + (high - low) // 2
		if check(mid, w, h, n):
			low = mid + 1
		else:
			high = mid
	return low

def check(beauty, s, n, count):
	pass
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
alphs = 'abcdefghijklmnopqrstuvwxyz'
from itertools import permutations
import math
import bisect as bis
import random
import sys
import collections as collect

def solve():
	(n, s) = read()
	time = []
	for i in range(n):
		(h, m) = read()
		time.append(h * 60 + m)
	if time[0] - 0 >= s + 1:
		print(0, 0)
		return
	ans = time[-1] + s + 1
	for i in range(0, n - 1):
		if time[i + 1] - time[i] >= 2 * (s + 1):
			ans = time[i] + s + 1
			break
	print(ans // 60, ans % 60)
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
for i in range(1):
	solve()


a = input().split()
s = int(a[1])
a[0] = int(a[0])
b = []
c = []
n = 0
for i in range(a[0]):
	x = input().split()
	b.append(int(x[0]) * 60 + int(x[1]))
for i in range(a[0] - 1):
	c.append(b[i + 1] - b[i])
if b[0] > s:
	print('0 0')
else:
	for i in range(a[0] - 1):
		if c[i] >= 2 * (s + 1):
			print(str((b[i] + s + 1) // 60) + ' ' + str((b[i] + s + 1) % 60))
			n = 1
			break
	if n == 0:
		print(str((b[-1] + s + 1) // 60) + ' ' + str((b[-1] + s + 1) % 60))


(n, s) = map(int, input().split())
l = list()
for i in range(n):
	(a, b) = map(int, input().split())
	v = a * 60 + b
	l.append(v)
value = s * 2 + 2
if s + 1 <= l[0]:
	print('0 0')
else:
	f = True
	for i in range(1, n):
		if l[i] - l[i - 1] >= value:
			zz = l[i - 1] + value // 2
			zx = zz // 60
			zzz = zz % 60
			print(zx, zzz, sep=' ')
			f = False
			break
	if f == True:
		zz = l[n - 1] + value // 2
		zx = zz // 60
		zzz = zz % 60
		print(zx, zzz, sep=' ')


(n, s) = map(int, input().split())
times = [0]
for i in range(n):
	(h, m) = map(int, input().split())
	times.append(60 * h + m)
del_ = [el - times[i] for (i, el) in enumerate(times[1:])]
if del_[0] >= s + 1:
	print(0, 0)
	exit()
for (i, el) in enumerate(del_):
	if el >= 2 * (s + 1):
		print(*divmod(times[i] + s + 1, 60))
		exit()
print(*divmod(times[-1] + s + 1, 60))


import sys
(n, se) = map(int, input().split())
s = [-se - 1]
for i in range(n):
	(h, m) = map(int, input().split())
	s.append(60 * h + m)
for i in range(1, len(s)):
	b = s[i]
	a = s[i - 1]
	if a + 1 + se + 1 + se <= b:
		print((a + 1 + se) // 60, (a + 1 + se) % 60)
		sys.exit()
print((s[-1] + 1 + se) // 60, (s[-1] + 1 + se) % 60)


(n, s) = list(map(int, input().split()))
(h, m) = list(map(int, input().split()))
perTime = h * 60 + m
can = True
if perTime > s:
	print('{} {}'.format(0, 0))
else:
	for _ in range(n - 1):
		(h, m) = list(map(int, input().split()))
		if h * 60 + m - perTime >= 2 * s + 2 and can:
			Time = int(perTime + s + 1)
			print('{} {}'.format(Time // 60, Time % 60))
			can = False
			break
		perTime = h * 60 + m
	if can:
		Time = int(perTime + s + 1)
		print('{} {}'.format(Time // 60, Time % 60))


(n, s) = map(int, input().split(' '))
a = list()
for i in range(0, n):
	a.append([int(x) for x in list(input().split(' '))])
a = sorted(a)
mn = list()
for i in range(0, len(a)):
	mn.append(a[i][0] * 60 + a[i][1])
mn.append(0)
mn = sorted(mn)
l = []
for i in range(1, len(mn)):
	l.append(mn[i] - mn[i - 1])
for i in range(0, len(l)):
	if i == 0:
		if l[0] >= s + 1:
			print('0 0')
			break
	if l[i] >= 2 * s + 2:
		ans = mn[i] + s + 1
		hour = ans // 60
		minute = ans % 60
		print(str(hour) + ' ' + str(minute))
		break
	if i == len(l) - 1:
		ans = mn[i + 1] + s + 1
		hour = ans // 60
		minute = ans % 60
		print(str(hour) + ' ' + str(minute))


(n, s) = map(int, input().split())
time = [0] * n
for i in range(n):
	(h, m) = map(int, input().split())
	time[i] = h * 60 + m
gogo = 0
if time[0] - 1 - gogo >= s:
	print(0, 0)
	exit()
fail = True
for i in range(n - 1):
	gogo = time[i] + 1 + s
	if time[i + 1] - gogo - 1 >= s:
		ans = gogo
		fail = False
		break
if fail:
	gogo = time[-1] + s + 1
hour = gogo // 60
min = gogo % 60
print(hour, min)


from typing import List

def time_diff(h1: int, m1: int, h2: int, m2: int) -> int:
	t1 = h1 * 60 + m1
	t2 = h2 * 60 + m2
	t = t2 - t1
	return t

def increas_time(h: int, m: int, t: int):
	t += h * 60 + m + 1
	return (t // 60, t % 60)

def solve(arr: List[List[int]], n: int, s: int):
	if arr[0] != [0, 0]:
		(h1, m1) = [0, 0]
		(h2, m2) = arr[0]
		diff = time_diff(h1, m1, h2, m2)
		if diff >= s + 1:
			return ' '.join(map(str, [0, 0]))
	for i in range(n - 1):
		(h1, m1) = arr[i]
		(h2, m2) = arr[i + 1]
		diff = time_diff(h1, m1, h2, m2)
		if diff >= 2 * s + 2:
			return ' '.join(map(str, increas_time(h1, m1, s)))
	return ' '.join(map(str, increas_time(*arr[-1], s)))

def main():
	(n, s) = list(map(int, input().split(' ')))
	res = []
	for _ in range(n):
		arr = list(map(int, input().split(' ')))
		res.append(arr)
	print(solve(res, n, s))
main()


from collections import deque, defaultdict, Counter
from itertools import product, groupby, permutations, combinations
from math import gcd, floor, inf, log2, sqrt, log10
from bisect import bisect_right, bisect_left
(n, t) = map(int, input().split())
prev = 0
ans = -1
first = True
for _ in range(n):
	(hour, minute) = map(int, input().split())
	cur = hour * 60 + minute
	if first:
		if cur > t:
			ans = 0
			break
		first = False
	if cur - prev >= t * 2 + 1:
		ans = prev + t
		break
	else:
		prev = cur + 1
if ans == -1:
	ans = cur + t + 1
print(ans // 60, ans % 60)


def canLand(i, s, arr):
	for a in range(len(arr)):
		if arr[a] - s > i:
			if a == 0:
				return True
			elif arr[a - 1] + s < i:
				return True
			else:
				return False
(N, s) = [int(k) for k in input().split(' ')]
landings = []
for i in range(N):
	(h, m) = [int(k) for k in input().split(' ')]
	landings.append(60 * h + m)
i = 0
while not canLand(i, s, landings):
	if i > 24 * 60:
		i = landings[-1] + s + 1
		break
	i += 1
print('{} {}'.format(i // 60, i % 60))


(n, s) = map(int, input().split())
a = []
for i in range(n):
	q = list(map(int, input().split()))
	a.append(q[0] * 60 + q[1])
if a[0] > s:
	print(0, 0)
	exit()
for j in range(1440):
	for i in range(n - 1):
		if a[i] + s < j < a[i + 1] - s:
			print(j // 60, j % 60)
			exit()
j = a[n - 1] + s + 1
print(j // 60, j % 60)


(n, s) = map(int, input().split())
pt = 0
for i in range(n):
	(h, m) = map(int, input().split())
	t = h * 60 + m
	if t > pt + s:
		break
	pt = s + t + 1
print(pt // 60, pt % 60)


(N, S) = map(int, input().split())
time = []
for n in range(N):
	(h, m) = map(int, input().split())
	a = h * 60
	z = a + m + 1
	time.append(z)
ans = 0
f = 0
if S + 1 < time[0]:
	print(0, 0)
else:
	for i in range(len(time) - 1):
		if time[i + 1] - time[i] > 2 * S + 1:
			ans = time[i]
			f = 1
			break
	if f == 1:
		ans += S
		hh = ans // 60
		mm = ans % 60
		print(hh, mm)
	else:
		j = time[-1]
		j += S
		hh = j // 60
		mm = j % 60
		print(hh, mm)


def to_minutes(h, m):
	return h * 60 + m

def to_hours(m):
	return (m // 60, m % 60)

def solve():
	(n, s) = map(int, input().split(' '))
	(h, m) = map(int, input().split(' '))
	prev_arrival = to_minutes(h, m)
	if prev_arrival >= s + 1:
		return '0 0'
	for i in range(1, n):
		(h, m) = map(int, input().split(' '))
		arrival = to_minutes(h, m)
		if arrival - prev_arrival >= s * 2 + 2:
			return str(to_hours(prev_arrival + s + 1)[0]) + ' ' + str(to_hours(prev_arrival + s + 1)[1])
		prev_arrival = arrival
	return str(to_hours(prev_arrival + s + 1)[0]) + ' ' + str(to_hours(prev_arrival + s + 1)[1])
print(solve())


(n, s) = map(int, input().split())
ar = []
val = 2 * s + 2
for i in range(n):
	t = tuple(map(int, input().split()))
	ar.append(t[1] + t[0] * 60)
if ar[0] != 0 and ar[0] >= s + 1:
	print(0, 0)
else:
	flag = 0
	for i in range(1, n):
		if ar[i] - ar[i - 1] >= val:
			k = ar[i - 1] + (s + 1)
			print(k // 60, k % 60)
			flag = 1
			break
	if flag == 0:
		k = ar[n - 1] + (s + 1)
		print(k // 60, k % 60)


(n, s) = map(int, input().split())
hm = [-s - 1]
for i in range(n):
	(a, b) = map(int, input().split())
	hm.append(a * 60 + b)
i = 1
while i <= n:
	d = hm[i] - hm[i - 1] - 1
	if d >= 2 * s + 1:
		break
	i += 1
ss = hm[i - 1] + s + 1
hh = ss // 60
mm = ss % 60
print('%i %i' % (hh, mm))


(n, s) = map(int, input().split())
times = []
result = 0
need = True
for i in range(n):
	(h, m) = map(int, input().split())
	times.append(60 * h + m)
if n == 1:
	if 0 + s + 1 <= times[0]:
		need = False
for i in range(n - 1):
	if 0 + s + 1 <= times[0]:
		need = False
		break
	if times[i + 1] - times[i] >= 2 + 2 * s:
		result = times[i] + 1 + s
		break
if result == 0 and need:
	result = times[n - 1] + 1 + s
hour = result // 60
minute = result % 60
print(hour, minute)


(n, s) = [int(x) for x in input().split()]
t = []
for i in range(n):
	(h, m) = [int(x) for x in input().split()]
	t.append(h * 60 + m)
if t[0] >= s + 1:
	print(0, 0)
	exit()
for i in range(1, n):
	if t[i] - t[i - 1] >= 2 * s + 2:
		print((t[i - 1] + s + 1) // 60, (t[i - 1] + s + 1) % 60)
		exit()
print((t[-1] + s + 1) // 60, (t[-1] + s + 1) % 60)


(n, s) = list(map(int, input().split()))
(x1, x2) = list(map(int, input().split()))
if x1 * 60 + x2 + 1 > s + 1:
	print(0, 0)
	exit()
for i in range(1, n):
	(y1, y2) = list(map(int, input().split()))
	if (y1 - x1) * 60 + (y2 - x2) >= 2 * s + 2:
		t = x1 * 60 + x2 + s + 1
		print(t // 60, t % 60)
		exit()
	(x1, x2) = (y1, y2)
t = x1 * 60 + x2 + s + 1
print(t // 60, t % 60)


(n, s) = map(int, input().strip().split())
times = [[int(x) for x in input().strip().split()] for _ in range(n)]
slots = [0] * (24 * 60 * 30)
for (h, m) in times:
	start = 60 * h + m
	slots[start:start + 1 + s] = [1] * (s + 1)
c = 0
for k in range(len(slots)):
	if slots[k] != 0:
		ok = False
		c = 0
		continue
	c += 1
	if c == s + 1:
		a = k - s
		print('{} {}'.format(a // 60, a % 60))
		break


(n, S) = map(int, input().split())
s = S * 2 + 1
(h1, m1) = map(int, input().split())
if h1 * 60 + m1 > S:
	res = 0
else:
	res = -1
(lst, mas) = ([h1 * 60 + m1], [[h1, m1]])
for i in range(n - 1):
	(h2, m2) = map(int, input().split())
	mas.append([h2, m2])
	diff = h2 * 60 + m2
	lst.append(diff)
if res == -1:
	for i in range(n - 1):
		x = lst[i + 1] - lst[i]
		if x - s - 1 >= 0:
			res = lst[i] + S + 1
			break
	if res == -1:
		res = lst[-1] + S + 1
	print(res // 60, res % 60)
else:
	print(0, 0)


(n, s) = map(int, input().split())
a = []
(ansh, ansm) = (0, 0)
f = 0
f1 = 0
for u in range(n):
	S = input()
	a.append(S)
(h1, m1) = (0, 0)
ansh = h1
ansm = m1
d = a[0].split()
h2 = int(d[0])
m2 = int(d[1])
m1 += 1
h3 = h2 - h1
m3 = m2 - m1
if m3 < 0:
	m3 += 60
	h3 -= 1
tim = h3 * 60 + m3
if tim >= s:
	f = 2
	if ansm > 59:
		ansm -= 60
		ansh += 1
	print(ansh, ansm)
if f != 2:
	for u in range(1, n):
		d = a[u].split()
		e = a[u - 1].split()
		h2 = int(d[0])
		h1 = int(e[0])
		m2 = int(d[1])
		m1 = int(e[1])
		m1 += 1
		m1 += s
		if m1 > 60:
			m1 -= 60
			h1 += 1
		ansh = h1
		ansm = m1
		m1 += 1
		h3 = h2 - h1
		m3 = m2 - m1
		if m3 < 0:
			m3 += 60
			h3 -= 1
		tim = h3 * 60 + m3
		if tim >= s:
			f1 = 1
			if ansm > 59:
				ansm -= 60
				ansh += 1
			print(ansh, ansm)
			break
if f1 != 1 and f != 2:
	d = a[n - 1].split()
	(h2, m2) = (0, 0)
	h1 = int(d[0])
	m1 = int(d[1])
	m1 += 1
	m1 += s
	if m1 > 60:
		m1 -= 60
		h1 += 1
	ansh = h1
	ansm = m1
	m1 += 1
	h3 = h2 - h1
	m3 = m2 - m1
	if m3 < 0:
		m3 += 60
		h3 -= 1
	tim = h3 * 60 + m3
	if ansm > 59:
		ansm -= 60
		ansh += 1
	print(ansh, ansm)


(a, b) = map(int, input().split())
c = [-b - 1]
for i in range(a):
	(k, l) = map(int, input().split())
	c.append(k * 60 + l)
c.append(10000000000000000)
for i in range(a + 1):
	if c[i + 1] - c[i] > 2 * b + 1:
		u = c[i] + b + 1
		print(u // 60, u % 60)
		break


import math
import random
import heapq, bisect
import sys
from collections import deque, defaultdict
from fractions import Fraction
import sys
import threading
from collections import defaultdict
mod = 10 ** 9 + 7
mod1 = 998244353
import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')

class TreeNode:

	def __init__(self, k, v):
		self.key = k
		self.value = v
		self.left = None
		self.right = None
		self.parent = None
		self.height = 1
		self.num_left = 1
		self.num_total = 1

class AvlTree:

	def __init__(self):
		self._tree = None

	def add(self, k, v):
		if not self._tree:
			self._tree = TreeNode(k, v)
			return
		node = self._add(k, v)
		if node:
			self._rebalance(node)

	def _add(self, k, v):
		node = self._tree
		while node:
			if k < node.key:
				if node.left:
					node = node.left
				else:
					node.left = TreeNode(k, v)
					node.left.parent = node
					return node.left
			elif node.key < k:
				if node.right:
					node = node.right
				else:
					node.right = TreeNode(k, v)
					node.right.parent = node
					return node.right
			else:
				node.value = v
				return

	@staticmethod
	def get_height(x):
		return x.height if x else 0

	@staticmethod
	def get_num_total(x):
		return x.num_total if x else 0

	def _rebalance(self, node):
		n = node
		while n:
			lh = self.get_height(n.left)
			rh = self.get_height(n.right)
			n.height = max(lh, rh) + 1
			balance_factor = lh - rh
			n.num_total = 1 + self.get_num_total(n.left) + self.get_num_total(n.right)
			n.num_left = 1 + self.get_num_total(n.left)
			if balance_factor > 1:
				if self.get_height(n.left.left) < self.get_height(n.left.right):
					self._rotate_left(n.left)
				self._rotate_right(n)
			elif balance_factor < -1:
				if self.get_height(n.right.right) < self.get_height(n.right.left):
					self._rotate_right(n.right)
				self._rotate_left(n)
			else:
				n = n.parent

	def _remove_one(self, node):
		replacement = node.left or node.right
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = replacement
			else:
				node.parent.right = replacement
			replacement.parent = node.parent
			node.parent = None
		else:
			self._tree = replacement
			replacement.parent = None
		node.left = None
		node.right = None
		node.parent = None
		self._rebalance(replacement)

	def _remove_leaf(self, node):
		if node.parent:
			if AvlTree._is_left(node):
				node.parent.left = None
			else:
				node.parent.right = None
			self._rebalance(node.parent)
		else:
			self._tree = None
		node.parent = None
		node.left = None
		node.right = None

	def remove(self, k):
		node = self._get_node(k)
		if not node:
			return
		if AvlTree._is_leaf(node):
			self._remove_leaf(node)
			return
		if node.left and node.right:
			nxt = AvlTree._get_next(node)
			node.key = nxt.key
			node.value = nxt.value
			if self._is_leaf(nxt):
				self._remove_leaf(nxt)
			else:
				self._remove_one(nxt)
			self._rebalance(node)
		else:
			self._remove_one(node)

	def get(self, k):
		node = self._get_node(k)
		return node.value if node else -1

	def _get_node(self, k):
		if not self._tree:
			return None
		node = self._tree
		while node:
			if k < node.key:
				node = node.left
			elif node.key < k:
				node = node.right
			else:
				return node
		return None

	def get_at(self, pos):
		x = pos + 1
		node = self._tree
		while node:
			if x < node.num_left:
				node = node.left
			elif node.num_left < x:
				x -= node.num_left
				node = node.right
			else:
				return (node.key, node.value)
		raise IndexError('Out of ranges')

	@staticmethod
	def _is_left(node):
		return node.parent.left and node.parent.left == node

	@staticmethod
	def _is_leaf(node):
		return node.left is None and node.right is None

	def _rotate_right(self, node):
		if not node.parent:
			self._tree = node.left
			node.left.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.left
			node.left.parent = node.parent
		else:
			node.parent.right = node.left
			node.left.parent = node.parent
		bk = node.left.right
		node.left.right = node
		node.parent = node.left
		node.left = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	def _rotate_left(self, node):
		if not node.parent:
			self._tree = node.right
			node.right.parent = None
		elif AvlTree._is_left(node):
			node.parent.left = node.right
			node.right.parent = node.parent
		else:
			node.parent.right = node.right
			node.right.parent = node.parent
		bk = node.right.left
		node.right.left = node
		node.parent = node.right
		node.right = bk
		if bk:
			bk.parent = node
		node.height = max(self.get_height(node.left), self.get_height(node.right)) + 1
		node.num_total = 1 + self.get_num_total(node.left) + self.get_num_total(node.right)
		node.num_left = 1 + self.get_num_total(node.left)

	@staticmethod
	def _get_next(node):
		if not node.right:
			return node.parent
		n = node.right
		while n.left:
			n = n.left
		return n

class SegmentTree1:

	def __init__(self, data, default=300006, func=lambda a, b: min(a, b)):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class SegmentTree:

	def __init__(self, data, default=0, func=lambda a, b: a + b):
		self._default = default
		self._func = func
		self._len = len(data)
		self._size = _size = 1 << (self._len - 1).bit_length()
		self.data = [default] * (2 * _size)
		self.data[_size:_size + self._len] = data
		for i in reversed(range(_size)):
			self.data[i] = func(self.data[i + i], self.data[i + i + 1])

	def __delitem__(self, idx):
		self[idx] = self._default

	def __getitem__(self, idx):
		return self.data[idx + self._size]

	def __setitem__(self, idx, value):
		idx += self._size
		self.data[idx] = value
		idx >>= 1
		while idx:
			self.data[idx] = self._func(self.data[2 * idx], self.data[2 * idx + 1])
			idx >>= 1

	def __len__(self):
		return self._len

	def query(self, start, stop):
		if start == stop:
			return self.__getitem__(start)
		stop += 1
		start += self._size
		stop += self._size
		res = self._default
		while start < stop:
			if start & 1:
				res = self._func(res, self.data[start])
				start += 1
			if stop & 1:
				stop -= 1
				res = self._func(res, self.data[stop])
			start >>= 1
			stop >>= 1
		return res

	def __repr__(self):
		return 'SegmentTree({0})'.format(self.data)

class Factorial:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorials = [1, 1]
		self.invModulos = [0, 1]
		self.invFactorial_ = [1, 1]

	def calc(self, n):
		if n <= -1:
			print('Invalid argument to calculate n!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.factorials):
			return self.factorials[n]
		nextArr = [0] * (n + 1 - len(self.factorials))
		initialI = len(self.factorials)
		prev = self.factorials[-1]
		m = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * i % m
		self.factorials += nextArr
		return self.factorials[n]

	def inv(self, n):
		if n <= -1:
			print('Invalid argument to calculate n^(-1)')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		p = self.MOD
		pi = n % p
		if pi < len(self.invModulos):
			return self.invModulos[pi]
		nextArr = [0] * (n + 1 - len(self.invModulos))
		initialI = len(self.invModulos)
		for i in range(initialI, min(p, n + 1)):
			next = -self.invModulos[p % i] * (p // i) % p
			self.invModulos.append(next)
		return self.invModulos[pi]

	def invFactorial(self, n):
		if n <= -1:
			print('Invalid argument to calculate (n^(-1))!')
			print('n must be non-negative value. But the argument was ' + str(n))
			exit()
		if n < len(self.invFactorial_):
			return self.invFactorial_[n]
		self.inv(n)
		nextArr = [0] * (n + 1 - len(self.invFactorial_))
		initialI = len(self.invFactorial_)
		prev = self.invFactorial_[-1]
		p = self.MOD
		for i in range(initialI, n + 1):
			prev = nextArr[i - initialI] = prev * self.invModulos[i % p] % p
		self.invFactorial_ += nextArr
		return self.invFactorial_[n]

class Combination:

	def __init__(self, MOD):
		self.MOD = MOD
		self.factorial = Factorial(MOD)

	def ncr(self, n, k):
		if k < 0 or n < k:
			return 0
		k = min(k, n - k)
		f = self.factorial
		return f.calc(n) * f.invFactorial(max(n - k, k)) * f.invFactorial(min(k, n - k)) % self.MOD

def powm(a, n, m):
	if a == 1 or n == 0:
		return 1
	if n % 2 == 0:
		s = powm(a, n // 2, m)
		return s * s % m
	else:
		return a * powm(a, n - 1, m) % m

def sort_list(list1, list2):
	zipped_pairs = zip(list2, list1)
	z = [x for (_, x) in sorted(zipped_pairs)]
	return z

def product(l):
	por = 1
	for i in range(len(l)):
		por *= l[i]
	return por

def binarySearchCount(arr, n, key):
	left = 0
	right = n - 1
	count = 0
	while left <= right:
		mid = int((right + left) / 2)
		if arr[mid] <= key:
			count = mid + 1
			left = mid + 1
		else:
			right = mid - 1
	return count

def countdig(n):
	c = 0
	while n > 0:
		n //= 10
		c += 1
	return c

def binary(x, length):
	y = bin(x)[2:]
	return y if len(y) >= length else '0' * (length - len(y)) + y

def countGreater(arr, n, k):
	l = 0
	r = n - 1
	leftGreater = n
	while l <= r:
		m = int(l + (r - l) / 2)
		if arr[m] >= k:
			leftGreater = m
			r = m - 1
		else:
			l = m + 1
	return n - leftGreater

class TrieNode:

	def __init__(self):
		self.children = [None] * 26
		self.isEndOfWord = False

class Trie:

	def __init__(self):
		self.root = self.getNode()

	def getNode(self):
		return TrieNode()

	def _charToIndex(self, ch):
		return ord(ch) - ord('a')

	def insert(self, key):
		pCrawl = self.root
		length = len(key)
		for level in range(length):
			index = self._charToIndex(key[level])
			if not pCrawl.children[index]:
				pCrawl.children[index] = self.getNode()
			pCrawl = pCrawl.children[index]
		pCrawl.isEndOfWord = True

	def search(self, key):
		pCrawl = self.root
		length = len(key)
		for level in range(length):
			index = self._charToIndex(key[level])
			if not pCrawl.children[index]:
				return False
			pCrawl = pCrawl.children[index]
		return pCrawl != None and pCrawl.isEndOfWord

class Node:

	def __init__(self, data):
		self.data = data
		self.count = 0
		self.left = None
		self.right = None

class BinaryTrie:

	def __init__(self):
		self.root = Node(0)

	def insert(self, pre_xor):
		self.temp = self.root
		for i in range(31, -1, -1):
			val = pre_xor & 1 << i
			if val:
				if not self.temp.right:
					self.temp.right = Node(0)
				self.temp = self.temp.right
				self.temp.count += 1
			if not val:
				if not self.temp.left:
					self.temp.left = Node(0)
				self.temp = self.temp.left
				self.temp.count += 1
		self.temp.data = pre_xor

	def query(self, xor):
		self.temp = self.root
		for i in range(31, -1, -1):
			val = xor & 1 << i
			if not val:
				if self.temp.left and self.temp.left.count > 0:
					self.temp = self.temp.left
				elif self.temp.right:
					self.temp = self.temp.right
			elif self.temp.right and self.temp.right.count > 0:
				self.temp = self.temp.right
			elif self.temp.left:
				self.temp = self.temp.left
			self.temp.count -= 1
		return xor ^ self.temp.data
(n, s) = map(int, input().split())
l = [-s - 1]
for i in range(n):
	(a, b) = map(int, input().split())
	l.append(a * 60 + b)
for i in range(n):
	for j in range(l[i] + s + 1, l[i + 1] - s):
		print(j // 60, j % 60)
		sys.exit(0)
k = l[-1] + s + 1
print(k // 60, k % 60)


(n, s) = map(int, input().split())
b = [0] * 1440
for i in range(n):
	(h, m) = map(int, input().split())
	b[h * 60 + m] = 1
k = 0
prev = -s
ans = -1
for i in range(1440):
	if b[i] == 1:
		if i - prev >= 2 * s + 1:
			ans = prev + s
			break
		else:
			prev = i + 1
if ans == -1:
	print((prev + s) // 60, (prev + s) % 60)
else:
	print(ans // 60, ans % 60)


(n, s) = map(int, input().split())
times = []
for z in range(0, n):
	(h, m) = map(int, input().split())
	times.append(h * 60 + m)
if times[0] - (s + 1) >= 0:
	print('0 0')
else:
	for i in range(0, n):
		if i != n - 1:
			if times[i + 1] - times[i] - 2 >= 2 * s:
				ans = times[i] + 1 + s
				break
		else:
			ans = times[i] + s + 1
	ansM = ans % 60
	ansH = int((ans - ansM) / 60)
	print(ansH, ansM)


(n, s) = map(int, input().split())
takeOFF = 0
landing = []
for i in range(n):
	(land_h, land_m) = map(int, input().split())
	landing.append(land_h * 60 + land_m)
landing.append(2000)

def countHM(time):
	h = time // 60
	m = time % 60
	return (h, m)
if takeOFF + 1 + s <= landing[0]:
	print('0 0')
else:
	for i in range(n):
		if landing[i + 1] - landing[i] >= 2 + 2 * s:
			(h, m) = countHM(landing[i] + 1 + s)
			print(str(h) + ' ' + str(m))
			break


import sys
import math
(n, s) = map(int, input().split())
time = [0 for i in range(n)]
diffa = [0 for j in range(n)]
gap = 2 * s + 2
flag = False
for i in range(n):
	(h, m) = map(int, input().split())
	tota = h * 60 + m
	time[i] = tota
	if n == 1:
		if tota >= s + 1:
			print('0 0')
		else:
			hh = (tota + s + 1) // 60
			mm = (tota + s + 1) % 60
			print(hh, mm)
		break
	if time[0] > s:
		print('0 0')
		break
	if i > 0:
		diffa[i] = time[i] - time[i - 1]
		if diffa[i] >= gap:
			flag = True
			hh = (time[i - 1] + s + 1) // 60
			mm = (time[i - 1] + s + 1) % 60
			print(hh, mm)
			break
		if i == n - 1 and flag == False:
			hh = (time[n - 1] + s + 1) // 60
			mm = (time[n - 1] + s + 1) % 60
			print(hh, mm)


(n, s) = [int(i) for i in input().split()]
a = []
for i in range(n):
	(h, m) = [int(i) for i in input().split()]
	a.append(h * 60 + m + 1)
ans = 0
for i in range(n - 1):
	if a[i + 1] - a[i] > 2 * s + 1:
		ans = a[i] + s
		break
if a[0] > s + 1:
	ans = 0
elif ans == 0:
	ans = a[-1] + s
ans_h = ans // 60
ans_m = ans % 60
print(ans_h, ans_m)


R = lambda : map(int, input().split())
(n, k) = R()
a = 0
for _ in range(n):
	(h, m) = R()
	b = h * 60 + m
	if _ == 0:
		if b - a >= k + 1:
			print(0, 0)
			exit(0)
		else:
			a = b
	elif b - a >= k * 2 + 2:
		print((a + k + 1) // 60, (a + k + 1) % 60)
		exit(0)
	else:
		a = b
print((a + k + 1) // 60, (a + k + 1) % 60)


l = []
boole = True
(n, s) = map(int, input().split())
for i in range(n):
	(h, m) = map(int, input().split())
	l.append(60 * h + m)
if s + 1 <= l[0]:
	boole = False
	print(0, 0)
else:
	for j in range(n - 1):
		if l[j + 1] - l[j] >= 2 * s + 2:
			boole = False
			print((l[j] + s + 1) // 60, (l[j] + s + 1) % 60)
			break
	if boole:
		print((l[-1] + s + 1) // 60, (1 + l[-1] + s) % 60)


import sys

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return map(int, minp().split())
(n, s) = mints()
a = []
for i in range(n):
	(h, m) = mints()
	a.append(h * 60 + m)
if a[0] >= s + 1:
	print(0, 0)
else:
	for i in range(n - 1):
		if a[i + 1] - a[i] - 1 >= s * 2 + 1:
			print((a[i] + s + 1) // 60, (a[i] + s + 1) % 60)
			exit(0)
	print((a[-1] + 1 + s) // 60, (a[-1] + 1 + s) % 60)


(n, s) = map(int, input().split())
t = []
for i in range(n):
	(h, m) = map(int, input().split())
	t.append(60 * h + m)
(x, y) = (0, 0)
if t[0] < s + 1:
	for i in range(n - 1):
		if t[i + 1] - t[i] >= 2 * s + 2:
			x = t[i] + s + 1
			break
	if x == 0:
		x = t[n - 1] + s + 1
y = x % 60
x = x // 60
print(x, y)


(n, s) = map(int, input().split())
a = [[0, -1 * s - 1]]
x = 0
for i in range(n):
	a.append(list(map(int, input().split())))
for i in range(n):
	if 60 * (a[i + 1][0] - a[i][0]) + a[i + 1][1] - a[i][1] >= 2 * s + 2:
		d = a[i][1] + s + 1
		c = a[i][0]
		if d > 60:
			c += d // 60
			d %= 60
		break
else:
	d = a[-1][1] + s + 1
	c = a[-1][0]
	c += d // 60
	d = d % 60
print('{} {}'.format(c, d))