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

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)


(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())
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) = 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) = 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(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)


(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, 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)


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())
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())
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)


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 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())
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))