You are given a broken clock. You know, that it is supposed to show time in 12- or 24-hours HH:MM format. In 12-hours format hours change from 1 to 12, while in 24-hours it changes from 0 to 23. In both formats minutes change from 0 to 59.

You are given a time in format HH:MM that is currently displayed on the broken clock. Your goal is to change minimum number of digits in order to make clocks display the correct time in the given format.

For example, if 00:99 is displayed, it is enough to replace the second 9 with 3 in order to get 00:39 that is a correct time in 24-hours format. However, to make 00:99 correct in 12-hours format, one has to change at least two digits. Additionally to the first change one can replace the second 0 with 1 and obtain 01:39.


-----Input-----

The first line of the input contains one integer 12 or 24, that denote 12-hours or 24-hours format respectively.

The second line contains the time in format HH:MM, that is currently displayed on the clock. First two characters stand for the hours, while next two show the minutes.


-----Output-----

The only line of the output should contain the time in format HH:MM that is a correct time in the given format. It should differ from the original in as few positions as possible. If there are many optimal solutions you can print any of them.


-----Examples-----
Input
24
17:30

Output
17:30

Input
12
17:30

Output
07:30

Input
24
99:99

Output
09:09

Source: codeforces | Difficulty: EASY
Tags: Implementation

Complete search


Solutions:
def solve():
	n = int(input())
	arr = list(input())
	if int(arr[3]) > 5:
		arr[3] = '0'
	if n == 24:
		if int(arr[0] + arr[1]) > 23:
			arr[0] = '0'
	if n == 12:
		if int(arr[0] + arr[1]) > 12 or int(arr[0] + arr[1]) == 0:
			if arr[1] == '0':
				arr[0] = '1'
			else:
				arr[0] = '0'
	print(''.join(arr))
solve()


__author__ = 'Azad'
import collections

def same(a, b):
	a = list(a)
	b = list(b)
	return sum([1 * (a == b) for (a, b) in zip(a, b)])
f = int(input())
(h, m) = input().split(':')
if int(m) > 59:
	m = '0' + m[1]
if f == 24:
	l = ['0' + str(x) for x in range(0, 10, 1)]
	for x in range(10, 24, 1):
		l.append(str(x))
	e = {x: same(x, h) for x in l}
	stats = collections.Counter(e)
	print(stats.most_common(1)[0][0] + ':' + m)
else:
	l = ['0' + str(x) for x in range(1, 10, 1)]
	l.append('10')
	l.append('11')
	l.append('12')
	e = {x: same(x, h) for x in l}
	stats = collections.Counter(e)
	print(stats.most_common(1)[0][0] + ':' + m)


__author__ = 'Alexander'
import sys
format = int(sys.stdin.readline().strip())
(timeH, timeM) = map(int, sys.stdin.readline().split(':'))
if format == 12:
	if timeH > 12 or timeH == 0:
		if timeH == 0:
			timeH = 1
		elif timeH % 10 == 0:
			timeH = 10
		else:
			timeH %= 10
	if timeM > 59:
		timeM %= 10
else:
	if timeH > 23:
		timeH %= 10
	if timeM > 59:
		timeM %= 10
sys.stdout.write('%02d:%02d' % (timeH, timeM))


def change(n):
	d2 = n % 10
	n = n // 10
	d1 = n
	if d1 == 0 or d2 == 0:
		d1 = 1
	else:
		d1 = 0
	return d1 * 10 + d2
frmat = int(input())
time = input()
time = time.strip().split(':')
hh = int(time[0])
mm = int(time[1])
del time
if frmat == 12:
	if hh == 0 or hh > 12:
		hh = change(hh)
if frmat == 24:
	if hh > 23:
		hh = change(hh)
if mm > 59:
	mm = change(mm)
hrs = str(hh)
mins = str(mm)
if hh < 10:
	hrs = '0' + hrs
if mm < 10:
	mins = '0' + mins
print(f'{hrs}:{mins}')


def correct_hour(is24, a, b):
	if is24:
		if a > 2:
			a = 0
		elif a == 2 and b > 3:
			b = 0
	elif a == b == 0:
		b = 1
	elif a > 1:
		if b == 0:
			a = 1
		else:
			a = 0
	elif a == 1 and b > 2:
		b = 0
	return (a, b)

def correct_minutes(c, d):
	if c > 5:
		c = 0
	return (c, d)
is24 = input() == '24'
(a, b, _, c, d) = input()
(a, b) = correct_hour(is24, int(a), int(b))
(c, d) = correct_minutes(int(c), int(d))
print(f'{a}{b}:{c}{d}')


time_format = input()
(hours, minutes) = input().split(':')
if time_format == '12':
	if not 1 <= int(hours) <= 12:
		if hours[1] != '0':
			hours = '0' + hours[1]
		else:
			hours = '10'
if time_format == '24':
	if not 0 <= int(hours) <= 23:
		hours = '1' + hours[1]
if not 0 <= int(minutes) <= 59:
	minutes = '1' + str(minutes)[1]
print(':'.join([hours, minutes]))


import sys
n = int(input())
time = str(input())
if n == 12:
	if int(time[0:2]) == 0:
		time = time[:1] + '1' + time[2:]
	if int(time[0]) > 1:
		time = '1' + time[1:3] + time[3:]
	if int(time[0:2]) < 12 and int(time[3:]) < 60:
		print(time)
		sys.exit()
	if int(time[0:2]) > 12:
		time = '0' + time[1:3] + time[3:]
	if int(time[3:]) >= 60:
		time = time[:3] + '0' + time[4:]
	print(time)
	sys.exit()
elif n == 24:
	if int(time[0:2]) < 24 and int(time[3:]) < 60:
		print(time)
		sys.exit()
	if int(time[0:2]) > 23:
		time = '0' + time[1:3] + time[3:]
	if int(time[3:]) >= 60:
		time = time[:3] + '0' + time[4:]
	print(time)
	sys.exit()


import sys
formato = int(input())
numeros = list(map(int, input().split(':')))
min = 0
nMin = 2
if formato == 12:
	for i in range(1, 13):
		minAux = 0
		if numeros[0] % 10 != i % 10:
			minAux += 1
		if numeros[0] / 10 != i / 10:
			minAux += 1
		if minAux <= nMin:
			nMin = minAux
			min = i
else:
	for i in range(0, 24):
		minAux = 0
		if numeros[0] % 10 != i % 10:
			minAux += 1
		if numeros[0] / 10 != i / 10:
			minAux += 1
		if minAux <= nMin:
			nMin = minAux
			min = i
nMin = 2
minMin = 0
for i in range(0, 60):
	minAux = 0
	if numeros[1] % 10 != i % 10:
		minAux += 1
	if numeros[1] / 10 != i / 10:
		minAux += 1
	if minAux <= nMin:
		nMin = minAux
		minMin = i
print(str(min).zfill(2) + ':' + str(minMin).zfill(2))


def solvecase():
	form = int(input().strip())
	(hh, mm) = list(map(int, input().strip().split(':')))
	if mm > 59:
		mm = mm % 10
	if form == 24:
		if hh > 23:
			hh = hh % 10
	elif form == 12:
		if hh > 12:
			if hh % 10:
				hh = hh % 10
			else:
				hh = 10
		if hh == 0:
			hh = 1
	return '%02d:%02d' % (hh, mm)

def main():
	print(solvecase())
main()


import sys
import math

def norm(n):
	if n < 10:
		return '0' + str(n)
	else:
		return str(n)
f = int(sys.stdin.readline())
s = sys.stdin.readline()
h = int(s[:2])
m = int(s[3:])
if m > 59:
	m = 10 + m % 10
if f == 24:
	if h > 23:
		h = 10 + h % 10
	print(norm(h) + ':' + norm(m))
	exit()
if h > 12:
	h = h % 10
if h == 0:
	print('10:' + norm(m))
	exit()
print(norm(h) + ':' + norm(m))


n = int(input())
(h, m) = input().split(':')
ans = 1000
ans1 = ''
if n == 12:
	(h1, m1) = ('', '')
	for i in range(1, 13):
		for j in range(0, 60):
			h1 = (2 - len(str(i))) * '0' + str(i)
			m1 = (2 - len(str(j))) * '0' + str(j)
			cnt = 4
			if h1[0] == h[0]:
				cnt -= 1
			if h1[1] == h[1]:
				cnt -= 1
			if m1[0] == m[0]:
				cnt -= 1
			if m1[1] == m[1]:
				cnt -= 1
			if cnt < ans:
				ans = cnt
				ans1 = h1 + ':' + m1
	print(ans1)
else:
	(h1, m1) = ('', '')
	for i in range(24):
		for j in range(0, 60):
			h1 = (2 - len(str(i))) * '0' + str(i)
			m1 = (2 - len(str(j))) * '0' + str(j)
			cnt = 4
			if h1[0] == h[0]:
				cnt -= 1
			if h1[1] == h[1]:
				cnt -= 1
			if m1[0] == m[0]:
				cnt -= 1
			if m1[1] == m[1]:
				cnt -= 1
			if cnt < ans:
				ans = cnt
				ans1 = h1 + ':' + m1
	print(ans1)


def main():
	n = int(input())
	(hours, minutes) = input().split(':')
	hours_1 = ''
	minutes_1 = ''
	if n == 24:
		if int(hours) >= 24:
			hours_1 += '0' + hours[1]
		else:
			hours_1 += hours
		if int(minutes) > 59:
			minutes_1 += '0' + minutes[1]
		else:
			minutes_1 += minutes
	else:
		if int(hours) > 12 and int(hours) % 10 != 0:
			hours_1 += '0' + hours[1]
		elif int(hours) % 10 == 0:
			hours_1 += '10'
		else:
			hours_1 += hours
		if int(minutes) > 59:
			minutes_1 += '0' + minutes[1]
		else:
			minutes_1 += minutes
	print('{}:{}'.format(hours_1, minutes_1))
	return
main()


def main():
	formato = input()
	hora = input()
	if int(hora[3]) > 5:
		hora = hora[:3] + '0' + hora[4]
	if formato == '12':
		if int(hora[:2]) > 12 or hora[:2] == '00':
			if hora[1] != '0':
				hora = '0' + hora[1:]
			else:
				hora = '10' + hora[2:]
	if formato == '24':
		if int(hora[:2]) > 23:
			hora = '0' + hora[1:]
	print(hora)
	return 0
main()


n = input()
(hour, min) = map(int, input().split(':'))
if n == '12':
	if not 1 <= hour <= 12:
		hour %= 10
		if hour == 0:
			hour = 10
elif not 0 <= hour <= 23:
	hour %= 10
if not 0 <= min <= 59:
	min %= 10
print('{0:02d}:{1:02d}'.format(hour, min))


n = int(input())
s = input().split(':')
if n == 12:
	if s[0] == '00':
		s[0] = '01'
	elif int(s[0]) > 12 and s[0][1] == '0':
		s[0] = '10'
	elif int(s[0]) > 12:
		s[0] = '0' + s[0][1]
elif int(s[0]) > 23:
	s[0] = '0' + s[0][1]
if int(s[1]) > 59:
	s[1] = '0' + s[1][1]
print(':'.join(s))


t = input()
t = int(t)
(x, y) = input().split(':')
x = int(x)
y = int(y)
if t == 24:
	if x > 23:
		x = x % 10
		if y > 59:
			y = y % 10
	elif y > 59:
		y = y % 10
elif x == 0:
	x = 1
	if y > 59:
		y = y % 10
elif x > 12:
	if x % 10 == 0:
		x = 10
	else:
		x = x % 10
	if y > 59:
		y = y % 10
elif y > 59:
	y = y % 10
if y < 10:
	y = str(y)
	y = '0' + y
if x < 10:
	x = str(x)
	x = '0' + x
print(x, ':', y, sep='')


a = input()
s = input()
ind = s.index(':')
h = s[0:ind]
m = s[ind + 1:]
count = 0
if a == '12':
	if (int(h) >= 1 and int(h) <= 12) and (int(m) >= 0 and int(m) <= 59):
		print(s)
	else:
		if not (int(h) >= 1 and int(h) <= 12):
			if h[len(h) - 1] == '0':
				h = '' + '1' + h[len(h) - 1]
			else:
				h = '' + '0' + h[len(h) - 1]
		if not (int(m) >= 0 and int(m) <= 59):
			m = '' + '0' + m[1]
		print(h + ':' + m)
elif (int(h) >= 0 and int(h) <= 23) and (int(m) >= 0 and int(m) <= 59):
	print(s)
else:
	if not (int(h) >= 0 and int(h) <= 23):
		h = '' + '1' + h[1]
	if not (int(m) >= 0 and int(m) <= 59):
		m = '' + '0' + m[1]
	print(h + ':' + m)


n = int(input())
(h, m) = [int(i) for i in input().split(':')]
k = 0
if n == 12:
	if h > 12:
		if h // 10 > 1 or h % 10 > 2:
			if h % 10 == 0:
				h = 10
			else:
				h = h % 10
	if h == 0:
		h = 1
	if m > 59:
		m = 50 + m % 10
if n == 24:
	if h > 23:
		h = h % 10
	if m > 59:
		m = 50 + m % 10
if m < 10:
	m = '0' + str(m)
if h < 10:
	h = '0' + str(h)
h = str(h)
m = str(m)
print(h + ':' + m)


n = int(input())
time = input()
newstr = []
if n == 12 and time[0] == '0' and (time[1] == '0'):
	newstr.append(time[0])
	newstr.append('1')
elif int(time[0] + time[1]) > n or 24 == int(time[0] + time[1]) == n:
	if time[1] == '0' and n == 12:
		newstr.append('1')
		newstr.append(time[1])
	else:
		newstr.append('0')
		newstr.append(time[1])
else:
	newstr.append(time[0])
	newstr.append(time[1])
newstr.append(':')
if int(time[3] + time[4]) > 59:
	newstr.append('0')
	newstr.append(time[4])
else:
	newstr.append(time[3])
	newstr.append(time[4])
print(''.join(newstr))


c = input()
(h, m) = list(map(int, input().split(':')))
if m >= 60:
	m %= 10
if c == '12' and h == 0:
	h = 1
elif c == '12' and h > 12:
	if h % 10:
		h %= 10
	else:
		h = 10
elif c == '24' and h > 23:
	h %= 10
print('%02d:%02d' % (h, m))


format = input()
time = list(input())
if time[3] > '5':
	time[3] = '0'
if format == '24':
	if time[0] > '2':
		time[0] = '0'
	elif time[0] == '2' and time[1] > '3':
		time[1] = '0'
else:
	if time[0] > '1':
		time[0] = '1' if time[1] == '0' else '0'
	if time[0] == '1' and time[1] > '2':
		time[1] = '0'
	if time[0] == '0' and time[1] == '0':
		time[1] = '1'
print(*time, sep='')


mode = input()
t = input()
(h, m) = map(int, t.split(':'))
if m >= 60:
	new_m = 10 + m % 10
else:
	new_m = m
if mode == '24':
	if h >= 24:
		new_h = 10 + h % 10
	else:
		new_h = h
elif h == 0:
	new_h = 10
elif h > 12:
	if h % 10 == 0:
		new_h = 10
	else:
		new_h = h % 10
else:
	new_h = h
print('{:02}:{:02}'.format(new_h, new_m))


n = int(input())
time = input()
h = ''.join(time[0:2])
m = ''.join(time[3:5])
if n == 12:
	if int(h) < 1:
		h = '01'
	elif int(h) > 12:
		if h[1] != '0':
			h = '0' + h[1]
		else:
			h = '10'
elif n == 24:
	if int(h) > 23:
		h = '0' + h[1]
if int(m) > 59:
	m = '0' + m[1]
time = h + ':' + m
print(time)


n = int(input())
tm = tuple(map(int, input().split(':')))
cost = [[0 for i in range(100)] for j in range(100)]
for i in range(100):
	for j in range(100):
		(x, y) = (i, j)
		for k in range(2):
			cost[i][j] += x % 10 != y % 10
			x //= 10
			y //= 10
hr_range = list(range(1, 13))
if n == 24:
	hr_range = list(range(0, 24))
best_time = None
best_val = 123
for h in hr_range:
	for m in range(60):
		cur = cost[h][tm[0]] + cost[m][tm[1]]
		if cur < best_val:
			best_val = cur
			best_time = '{:02d}:{:02d}'.format(h, m)
print(best_time)


f = int(input())
h = input().split(':')
if int(h[0]) > f or int(h[0]) == 24:
	h[0] = '0' + h[0][1]
if int(h[0]) == 0 and f == 12:
	h[0] = '1' + h[0][1]
if int(h[1]) >= 60:
	h[1] = '0' + h[1][1]
print(f'{h[0]}:{h[1]}')


formats = int(input())
s = input()
if s == '00:30':
	print(s)
else:
	hour = int(s[0] + s[1])
	hours = s[0] + s[1]
	q = hours
	minute = int(s[3] + s[4])
	minutes = s[3] + s[4]
	if (hour > formats or hour == 24) or hour == 0:
		hour = int('0' + hours[1])
		hours = '0' + hours[1]
	if hours[1] == '0' and hours[0] != '1' and (hours[0] != '0'):
		hour = 9
	hours = str(hour)
	if len(hours) == 1:
		hours = '0' + hours
	if minute > 59:
		minutes = '0' + minutes[1]
	if hours == '00' and q[1] == '0':
		hours = '10'
	elif hours == '00':
		hours = '01'
	print(hours + ':' + minutes)


formt = int(input())
clock = input()
(fn, sn) = clock.split(':')
if formt == 24:
	if fn > '23':
		if fn[0] > '1':
			fn = '0' + fn[1]
elif fn > '12':
	if fn[1] != '0':
		if fn[0] >= '1':
			fn = '0' + fn[1]
	elif fn >= '10':
		fn = '10'
elif fn == '00':
	fn = '01'
if sn > '59':
	if sn[0] > '5':
		sn = '0' + sn[1]
print(fn + ':' + sn, end='')


def brocken_clock(n, s):
	t = list(s)
	if n == 24:
		if int(t[0]) > 2:
			t[0] = '1'
		if t[0] == '2' and int(t[1]) > 3:
			t[1] = '0'
	else:
		if int(t[0]) > 1:
			if t[1] == '0':
				t[0] = '1'
			else:
				t[0] = '0'
		if t[0] == '1' and int(t[1]) > 2:
			t[1] = '1'
		if t[0] == '0' and t[1] == '0':
			t[1] = '1'
	if int(t[3]) >= 6:
		t[3] = '0'
	return ''.join(t)
m = int(input())
z = input()
print(brocken_clock(m, z))


def relogio(formato, hora):
	a = int(hora.split(':')[0])
	b = int(hora.split(':')[1])
	if formato == 12:
		if a > 12 and a % 10 != 0:
			a = a % 10
		elif (a > 12 or a < 1) and a % 10 == 0:
			a = a % 10 + 10
	elif a > 23 and a % 10 != 0:
		a = a % 10
	elif a > 23 and a % 10 == 0:
		a = a % 10 + 10
	if b > 59 and b % 10 != 0:
		b = b % 10
	elif b > 59 and b % 10 == 0:
		b = b % 10 + 10
	return '%02d:%02d' % (a, b)
formato = int(input())
hora = input()
print(relogio(formato, hora))


a = int(input())
(h, m) = map(int, input().split(':'))
if m > 59:
	m = 10 + m % 10
if a == 24:
	if h > 23:
		h = h % 10
elif h > 12:
	h = h % 10
if a == 12 and h == 0:
	h = 10
s = str(h)
if h < 10:
	s = '0' + s
s += ':'
if m < 10:
	s += '0'
s += str(m)
print(s)


form = int(input())
(hs, ms) = input().split(':')
if int(ms) > 59:
	ms = '0' + ms[1]
if form == 24:
	if int(hs) > 23:
		hs = '0' + hs[1]
elif int(hs) > 12:
	if hs[1] == '0':
		hs = '10'
	else:
		hs = '0' + hs[1]
elif int(hs) == 0:
	hs = '01'
print(hs + ':' + ms)


formato = int(input())
(hora, minuto) = input().split(':')
hora = int(hora)
minuto = int(minuto)
out_hora = ''
out_minuto = ''
if formato == 12:
	formato = 13
if hora >= formato:
	hora -= hora // 10 * 10
if hora == 0 and formato == 13:
	hora = 10
if minuto >= 60:
	minuto -= minuto // 10 * 10
if hora < 10:
	out_hora += '0'
if minuto < 10:
	out_minuto += '0'
out_hora += str(hora)
out_minuto += str(minuto)
print(out_hora + ':' + out_minuto)


f = input()
a = input().split(':')
h = int(a[0])
m = int(a[1])
if f == '12':
	if h > 0 and h <= 12:
		b = a[0] + ':'
	elif h == 0:
		b = '10:'
	elif a[0][1] == '0':
		b = '10:'
	else:
		b = '0' + a[0][1] + ':'
elif h >= 0 and h < 24:
	b = a[0] + ':'
else:
	b = '0' + a[0][1] + ':'
if m > 59:
	b += '0' + a[1][1]
else:
	b += a[1]
print(b)


entrada = int(input(''))
hora = input('')
hora = hora.split(':')
if entrada == 12:
	if int(hora[0][0]) > 1:
		if int(hora[0][1]) == 0:
			hora[0] = '10'
		else:
			hora[0] = '0' + hora[0][1]
	if int(hora[0][0]) == 0:
		if int(hora[0][1]) == 0:
			hora[0] = '01'
	elif int(hora[0][0]) == 1:
		if int(hora[0][1]) > 2:
			hora[0] = '0' + hora[0][1]
elif entrada == 24:
	if int(hora[0][0]) > 2:
		hora[0] = '0' + hora[0][1]
	elif int(hora[0][0]) == 2:
		if int(hora[0][1]) >= 4:
			hora[0] = '1' + hora[0][1]
if int(hora[1][0]) > 5:
	hora[1] = '3' + hora[1][1]
print(hora[0] + ':' + hora[1])


ff = input()
(hh, mm) = input().split(':')
if int(mm) >= 60:
	mm = '0' + mm[1]
if ff == '12' and hh == '00':
	hh = '10'
elif ff == '12' and int(hh) > 12 and (hh[1] != '0'):
	hh = '0' + hh[1]
elif ff == '12' and int(hh) > 12 and (hh[1] == '0'):
	hh = '1' + hh[1]
elif ff == '24' and int(hh) >= 24:
	hh = '0' + hh[1]
print(hh, mm, sep=':')


form = input()
(hour, minute) = input().split(':')
if form == '12' and hour == '00':
	hour = '01'
elif int(hour) >= int(form):
	if not (form == '12' and hour == '12'):
		if int(hour[0]) > int(form[0]) and int(hour[1]) < int(form[1]):
			hour = str(int(form[0])) + hour[1]
		else:
			hour = str(int(form[0]) - 1) + hour[1]
if int(minute[0]) > 5:
	minute = '0' + minute[1]
print(hour + ':' + minute)


formato = int(input())
horario = list(map(str, input().split(':')))
digitos = []
for i in horario[0]:
	digitos.append(int(i))
for i in horario[1]:
	digitos.append(int(i))
if formato == 12:
	if digitos[0] == 0 and digitos[1] == 0 and (digitos[2] > 5):
		digitos[0] = 0
		digitos[1] = 1
		digitos[2] = 2
	elif digitos[0] >= 1 and digitos[1] > 2 and (digitos[2] > 5):
		digitos[0] = 0
		digitos[2] = 0
	elif digitos[0] == 0 and digitos[1] == 0:
		digitos[0] = 1
	elif digitos[0] == 1 and digitos[1] > 2:
		digitos[0] = 0
	elif digitos[0] > 1 and digitos[2] > 5:
		digitos[0] = 0
		digitos[2] = 0
	elif digitos[0] > 1:
		digitos[0] = 1
	elif digitos[2] > 5:
		digitos[2] = 0
elif formato == 24:
	if digitos[0] == 2 and digitos[1] >= 4 and (digitos[2] > 5):
		digitos[1] = 3
		digitos[2] = 0
	elif digitos[0] == 2 and digitos[1] >= 4:
		digitos[1] = 3
	elif digitos[0] > 2 and digitos[2] > 5:
		digitos[2] = 0
		digitos[0] = 0
	elif digitos[0] > 2 and digitos[2] > 5:
		digitos[0] = 0
		digitos[2] = 0
	elif digitos[0] > 2:
		digitos[0] = 0
	elif digitos[2] > 5:
		digitos[2] = 0
resposta = '{}{}:{}{}'.format(digitos[0], digitos[1], digitos[2], digitos[3])
print(resposta)


n = int(input())
(h, m) = input().split(':')
if m[0] >= '6':
	m = '0' + m[1]
if n == 12:
	if h[1] == '0' and h[0] != '1':
		h = '10'
	elif h > '12':
		h = '0' + h[1]
elif n == 24:
	if h >= '24':
		h = '1' + h[1]
print(h + ':' + m)


clock_format = int(input())
time = input()
H1 = int(time[0])
H2 = int(time[1])
M1 = int(time[3])
M2 = int(time[4])
if clock_format == 24:
	if H1 * 10 + H2 > 23:
		H1 = 0
	if M1 > 5:
		M1 = 0
	print(str(H1) + str(H2) + ':' + str(M1) + str(M2))
elif clock_format == 12:
	if H1 * 10 + H2 < 1 or H1 * 10 + H2 > 12:
		H1 = 1
		if H1 * 10 + H2 > 12:
			H1 = 0
	if H2 == 0 and H1 == 0:
		H2 = 1
	if M1 > 5:
		M1 = 0
	print(str(H1) + str(H2) + ':' + str(M1) + str(M2))


form = int(input())
clock = input().split(':')
tenHour = clock[0][0]
uniHour = clock[0][1]
tenMin = clock[1][0]
uniMin = clock[1][1]
if form == 12:
	if int(tenHour) * 10 + int(uniHour) > 12 and int(tenMin) < 6:
		if uniHour == '0':
			tenHour = 1
		else:
			tenHour = 0
	elif int(tenHour) * 10 + int(uniHour) > 12 and int(tenMin) >= 6:
		if uniHour == '0':
			tenHour = 1
		else:
			tenHour = 0
		tenMin = 0
	elif int(tenHour) * 10 + int(uniHour) == 0 and int(tenMin) < 6:
		uniHour = 1
	elif int(tenHour) * 10 + int(uniHour) == 0 and int(tenMin) >= 6:
		uniHour = 1
		tenMin = 0
	elif int(tenHour) * 10 + int(uniHour) <= 12 and int(tenMin) >= 6:
		tenMin = 0
elif int(tenHour) * 10 + int(uniHour) >= 24 and int(tenMin) < 6:
	tenHour = 0
elif int(tenHour) * 10 + int(uniHour) >= 24 and int(tenMin) >= 6:
	tenHour = 0
	tenMin = 0
elif int(tenHour) * 10 + int(uniHour) < 24 and int(tenMin) >= 6:
	tenMin = 0
print('{}{}:{}{}'.format(int(tenHour), int(uniHour), int(tenMin), uniMin))


q = int(input())
h = input()
g = h[0] + h[1]
n = h[3] + h[4]
if g == '24' and q == 24:
	g = '04'
if g == '00' and q == 12:
	g = '01'
if h[1] == '0' and q == 12:
	g = '10'
if int(g) > q:
	g = '0' + h[1]
if int(n) > 59:
	n = '0' + h[4]
print(g + ':' + n)


n = int(input())
hhmm = input()
res = list(hhmm)
hh = int(hhmm[:2])
mm = int(hhmm[3:])
if mm > 59:
	res[3] = '0'
if n == 12:
	if hh == 0:
		res[1] = '1'
	elif hh > 12:
		if res[1] != '0':
			res[0] = '0'
		else:
			res[0] = '1'
elif hh >= 24:
	res[0] = '0'
print(''.join(res))


r = range(24) if input() == '24' else range(1, 13)
s = input()
c = 100
ans = ''
for h in r:
	for m in range(60):
		t = '{0:02d}:{1:02d}'.format(h, m)
		l = sum((1 for (i, j) in zip(s, t) if i != j))
		if c > l:
			(c, ans) = (l, t)
print(ans)


format = input()
time = input().split(':')
if format == '12':
	if time[0] == '00':
		time[0] = '01'
	elif int(time[0]) > 12:
		if time[0][1] != '0':
			time[0] = '0' + time[0][1]
		else:
			time[0] = '10'
elif int(time[0]) > 23:
	time[0] = '0' + time[0][1]
if time[1][0] > '5':
	time[1] = '3' + time[1][1]
print(time[0] + ':' + time[1])


new_format = input()
(hours, minutes) = input().split(':')
if int(minutes[0]) > 5:
	minutes = '0' + minutes[1]
if new_format == '24':
	if int(hours[0]) > 2 or (hours[0] == '2' and int(hours[1]) >= 4):
		hours = '1' + hours[1]
else:
	if int(hours) > 12:
		if int(hours[0]) > 1:
			if hours[1] != '0':
				hours = '0' + hours[1]
			else:
				hours = '10'
		else:
			hours = '10'
	if hours == '00':
		hours = '01'
print(hours + ':' + minutes)


m = int(input())
l = list(map(str, input().split(':')))
d = ''
s = ''
if m == 24:
	if int(l[0]) > 23:
		s = '0' + l[0][1:]
	else:
		s = s + l[0]
	if int(l[1]) > 59:
		d = '0' + l[1][1:]
	else:
		d = d + l[1]
elif m == 12:
	if int(l[0]) > 12:
		if int(l[0][1]) == 0:
			s = '1' + l[0][1:]
		else:
			s = '0' + l[0][1:]
	elif int(l[0]) == 0:
		s = l[0][1:] + '1'
	elif int(l[0]) <= 12 and int(l[0]) != 0:
		s = s + l[0]
	if int(l[1]) > 59:
		d = '0' + l[1][1:]
	else:
		d = d + l[1]
print(s + ':' + d)


a = int(input())
b = list(map(int, input().split(':')))
out = ''
if b[1] // 10 >= 6:
	b[1] = 10 + b[1] % 10
elif b[1] < 10:
	b[1] = '0' + str(b[1])
if a == 12:
	if not 12 >= b[0] > 0:
		b[0] = '0' + str(b[0] % 10)
		if b[0] == '00':
			b[0] = '10'
elif not 23 >= b[0] > -1:
	b[0] = '0' + str(b[0] % 10)
if len(str(b[0])) == 1:
	b[0] = '0' + str(b[0])
print(str(b[0]) + ':' + str(b[1]))


import math as m

def main():
	time = int(input())
	(x, y) = [int(i) for i in input().split(':')]
	if y // 10 > 5:
		y = y % 10 + 50
	if time == 24:
		if x // 10 > 2:
			x = x % 10
		if x % 10 >= 4 and x // 10 >= 2:
			x = x - x % 10 + 3
	elif x == 0:
		x = 1
	elif x // 10 > 1:
		x = x % 10
		if x == 0:
			x += 10
	elif x % 10 > 2 and x // 10 >= 1:
		x = x - x % 10 + 1
	if x < 10:
		print('0', end='')
	print(x, ':', sep='', end='')
	if y < 10:
		print('0', end='')
	print(y)
main()


def f(x):
	if x == 0:
		return '00'
	elif len(str(x)) == 1:
		return '0' + str(x)
	else:
		return str(x)

def ch(a, b):
	q = 0
	for i in range(len(a)):
		q += int(a[i] != b[i])
	return q
tf = int(input())
time = [x for x in input().split(':')]
hr12 = []
hr24 = []
mins = []
for i in range(1, 13):
	hr12.append(f(i))
for i in range(24):
	hr24.append(f(i))
for i in range(60):
	mins.append(f(i))
ans = 10
if tf == 12:
	for x in hr12:
		ans = min(ans, ch(time[0], x))
else:
	for x in hr24:
		ans = min(ans, ch(time[0], x))
a2 = 10
for x in mins:
	a2 = min(a2, ch(x, time[1]))
fin = [None, None]
if tf == 12:
	for x in hr12:
		if ch(x, time[0]) == ans:
			fin[0] = x
else:
	for x in hr24:
		if ch(time[0], x) == ans:
			fin[0] = x
for x in mins:
	if ch(x, time[1]) == a2:
		fin[1] = x
print(':'.join(fin))


def norm1():
	global t1
	while True:
		if t1 > 23:
			t1 -= 10
		else:
			return t1

def norm2():
	global t1
	while True:
		if t1 > 12:
			t1 -= 10
		else:
			return t1
n = int(input())
(t1, t2) = map(int, input().split(':'))
if n == 12:
	if t2 >= 60:
		t2 = 40 + t2 % 10
	if t1 >= 13:
		norm2()
	if t1 == 0:
		t1 = 1
if n == 24:
	if t2 >= 60:
		t2 = 40 + t2 % 10
	if t1 >= 24:
		norm1()
if t1 < 10:
	t1 = '0' + str(t1)
if t2 < 10:
	t2 = '0' + str(t2)
print(t1, t2, sep=':')


fmt = int(input())
(hh, mm) = map(int, input().split(':'))
while mm > 59:
	mm -= 10
if fmt == 12 and hh > 12:
	while hh > 12:
		hh -= 10
elif fmt == 12 and hh == 0:
	hh = 1
elif fmt == 24 and hh > 23:
	while hh > 23:
		hh -= 10
print('%02d:%02d' % (hh, mm))


typee = int(input())
clock = input()
hours = clock[0:2]
minutes = clock[3:5]
if typee == 12:
	if int(hours) < 1 and int(minutes) >= 60:
		hours = hours[0] + str(1)
		minutes = str(int(minutes[0]) - int(minutes[0])) + minutes[1]
	elif int(hours) < 1 and int(minutes) < 60:
		hours = hours[0] + str(1)
	elif int(hours) <= 12 and int(minutes) < 60:
		hours = hours
		minutes = minutes
	elif int(hours) <= 12 and int(minutes) >= 60:
		hours = hours
		minutes = str(int(minutes[0]) - int(minutes[0])) + minutes[1]
	elif int(hours) > 12 and int(minutes) >= 60:
		hours = str(int(hours[0]) - int(hours[0])) + hours[1]
		if hours[1] == '0':
			hours = '10'
		minutes = str(int(minutes[0]) - int(minutes[0])) + minutes[1]
	elif int(hours) > 21:
		hours = str(int(hours[0]) - int(hours[0])) + hours[1]
		if hours[1] == '0':
			hours = '10'
	else:
		hours = str(int(hours[0]) - 1) + hours[1]
else:
	if int(hours) > 23:
		hours = str(int(hours[0]) - int(hours[0])) + hours[1]
	if int(minutes) >= 60:
		minutes = str(int(minutes[0]) - int(minutes[0])) + minutes[1]
print(hours + ':' + minutes)


f = input()
d = list(input())
if f == '12':
	if int(d[0]) > 1:
		if int(d[1]) > 2:
			d[0] = '0'
		else:
			d[0] = '1'
	if d[0] == '0' and d[1] == '0':
		d[0] = '0'
		d[1] = '1'
	if d[0] == '1' and int(d[1]) > 2:
		d[1] = '2'
if f == '24':
	if int(d[0]) > 2:
		d[0] = '0'
	if d[0] == '2' and int(d[1]) > 3:
		d[1] = '3'
if int(d[3]) > 5:
	d[3] = '0'
print(''.join(d))


formato = int(input())
a = input()
b = list(a)
if formato == 12:
	if 0 == int(b[0] + b[1]) or int(b[0] + b[1]) > 12:
		if b[1] == '0':
			b[0] = '1'
		else:
			b[0] = '0'
elif int(b[0] + b[1]) > 23:
	b[0] = '1'
if int(b[3] + b[4]) > 59:
	b[3] = '1'
hora = b[0] + b[1] + b[2] + b[3] + b[4]
print(hora)


N = int(input())
S = input().split(':')
H = S[0]
M = S[1]
res = 114514
ans = ''
if N == 12:
	for i in range(1, 13):
		for j in range(0, 60):
			h = str(i)
			m = str(j)
			while len(h) != 2:
				h = '0' + h
			while len(m) != 2:
				m = '0' + m
			diff = 0
			for (x, y) in zip(h, H):
				if x != y:
					diff += 1
			for (x, y) in zip(m, M):
				if x != y:
					diff += 1
			if diff < res:
				res = diff
				ans = h + ':' + m
else:
	for i in range(0, 24):
		for j in range(0, 60):
			h = str(i)
			m = str(j)
			while len(h) != 2:
				h = '0' + h
			while len(m) != 2:
				m = '0' + m
			diff = 0
			for (x, y) in zip(h, H):
				if x != y:
					diff += 1
			for (x, y) in zip(m, M):
				if x != y:
					diff += 1
			if diff < res:
				res = diff
				ans = h + ':' + m
print(ans)


from itertools import product
from functools import reduce
t = ['{0:02d}:{1:02d}'.format(h, m) for (h, m) in product(range(24) if input() == '24' else range(1, 13), range(60))]
s = input()
f = lambda t: sum((1 for (i, j) in zip(s, t) if i != j))
print(reduce(lambda x, y: x if f(x) < f(y) else y, t))


class CodeforcesTask722ASolution:

	def __init__(self):
		self.result = ''
		self.format = 0
		self.hour = ''

	def read_input(self):
		self.format = int(input())
		self.hour = input()

	def process_task(self):
		valid_minutes = [x for x in range(60)]
		valid_hours_12 = [x for x in range(1, 13)]
		valid_hours_24 = [x for x in range(24)]
		out_hour = list(self.hour)
		if self.format == 12:
			if int(self.hour[:2]) not in valid_hours_12:
				if self.hour[1] != '0':
					out_hour[0] = '0'
				elif self.hour[0] not in ['0', '1']:
					out_hour[0] = '1'
				else:
					out_hour[1] = '1'
		elif int(self.hour[:2]) not in valid_hours_24:
			out_hour[0] = '1'
		if int(self.hour[3:]) not in valid_minutes:
			out_hour[3] = '1'
		self.result = ''.join(out_hour)

	def get_result(self):
		return self.result
Solution = CodeforcesTask722ASolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())


t = input()
(h, m) = input().split(':')
if t == '12' and h[1] == '0' and (h != '10'):
	h = '10'
elif t == '12' and int(h) > 12 or (t == '24' and int(h) >= 24):
	h = '0' + h[1]
if int(m) >= 60:
	m = '0' + m[1]
print(h, ':', m, sep='')


t = input()
A = input()
an = list(A)
if t == '24':
	if int(A[-2:]) > 59:
		an[-2] = '0'
	if int(A[:2]) > 23:
		an[0] = '0'
else:
	if int(A[-2:]) > 59:
		an[-2] = '0'
	if int(A[:2]) > 12 and A[1] == '0':
		an[0] = '1'
	elif int(A[:2]) > 12 and A[1] != '0':
		an[0] = '0'
	elif int(A[:2]) == 0:
		an[0] = '1'
print(''.join(an))


c = int(input())
s = str(input().split())
a = int(s[2:4])
b = int(s[5:7])
l = list(s)
if c == 12:
	if a == 0:
		l[2] = '1'
	if a > 12:
		if l[3] != '0':
			l[2] = '0'
		else:
			l[2] = '1'
elif c == 24:
	if a >= 24:
		l[2] = '1'
if b >= 60:
	l[5] = '1'
print(''.join(l[2:4]) + ':' + ''.join(l[5:7]))


from collections import defaultdict
import sys, os, math
s = input()
time = input()
ans = ''
num = 0
num += int(time[1])
num += 10 * int(time[0])
if s == '12':
	if num >= 1 and num <= 12:
		ans += time[:2]
	elif time[1] == '0':
		ans += '10'
	else:
		ans += '0' + time[1]
elif num >= 0 and num <= 23:
	ans += time[:2]
else:
	ans += '0' + time[1]
ans += ':'
if time[3] not in '012345':
	ans += '0'
else:
	ans += time[3]
ans += time[4]
print(ans)


def main():
	try:
		while True:
			n = input()
			(a, b) = input().split(':')
			(c, d) = b
			(a, b) = a
			if c >= '6':
				c = '5'
			if n == '24':
				if a == '2':
					if b >= '4':
						b = '3'
				elif a >= '3':
					a = '1'
			elif a == b == '0':
				b = '1'
			elif a == '1':
				if b >= '3':
					b = '2'
			elif a >= '2':
				if b == '0':
					a = '1'
				else:
					a = '0'
			print('%s%s:%s%s' % (a, b, c, d))
	except EOFError:
		pass
main()


F = input()
is24 = True
if F == '12':
	is24 = False
Time = input()
H = int(Time[0:2])
M = int(Time[3:5])
if is24:
	if H > 23:
		H = 10 + H % 10
	if M > 59:
		M = 10 + M % 10
else:
	if H > 12:
		if H % 10 <= 2:
			H = 10 + H % 10
		else:
			H = H % 10
	if H == 0:
		H = 1
	if M > 59:
		M = 10 + M % 10
print(str(H).zfill(2) + ':' + str(M).zfill(2))


n = int(input())
s = input()
h = int(s[0]) * 10 + int(s[1])
m = int(s[3]) * 10 + int(s[4])
t = list(s)
if n == 24:
	if h > 23:
		t[0] = 1
	if m > 59:
		t[3] = 1
else:
	if h > 12:
		if int(t[1]) >= 2:
			t[0] = 0
		elif int(t[1]) < 2:
			t[0] = 1
	elif h == 0:
		t[1] = 1
	if m > 59:
		t[3] = 1
print(''.join(map(str, t)))


f = int(input())
s = input()
s = s[:2] + s[3:]
s = [int(i) for i in s]
if f == 24:
	if int(str(s[0]) + str(s[1])) > 23:
		if s[0] > 2:
			s[0] = 0
		else:
			s[1] = 0
	if s[2] > 5:
		s[2] = 0
if f == 12:
	if s[0] == 1:
		if s[1] > 2:
			s[1] = 0
	elif s[0] == 0:
		if s[1] == 0:
			s[1] = 1
	elif s[1] > 2:
		s[0] = 0
	else:
		s[0] = 1
	if s[2] > 5:
		s[2] = 0
print(s[0], end='')
print(s[1], end='')
print(':', end='')
print(s[2], end='')
print(s[3], end='')


import sys
frmt = int(sys.stdin.readline())
(hh, mm) = map(int, sys.stdin.read().split(':'))
mms = ''
if mm > 59:
	mms = '5' + str(mm % 10)
else:
	mms = str(mm).zfill(2)
if frmt == 24:
	frmt -= 1
hhs = ''
if hh > frmt:
	hhs = '0' + str(hh % 10)
	if hhs == '00':
		hhs = '10'
elif frmt == 12 and hh == 0:
	hhs = '01'
else:
	hhs = str(hh).zfill(2)
print(hhs + ':' + mms)


n = int(input())
x = list(input())
if int(x[3]) > 5:
	x[3] = '3'
if n == 12:
	if x[1] == '0':
		if x[0] == '0':
			x[1] = '1'
	if x[0] != '1' and x[0] != '0':
		if x[1] == '0':
			x[0] = '1'
		else:
			x[0] = '0'
	if int(x[1]) > 2:
		if x[0] == '1':
			x[1] = '0'
elif x[0] == '2':
	if int(x[1]) > 3:
		x[0] = '0'
elif int(x[0]) > 2:
	x[0] = '0'
print(''.join(x))


n = int(input())
s = input()
(hr, mn) = (s[:2], s[3:])
(h, m) = (int(hr), int(mn))
if n == 24:
	if h < 24 and m < 60:
		print(hr + ':' + mn)
	elif h >= 24 and m > 60:
		print('0' + hr[1] + ':0' + mn[1])
	elif m >= 60:
		print(hr + ':0' + mn[1])
	else:
		print('0' + hr[1] + ':' + mn)
elif h < 13 and h > 0 and (m < 60):
	print(hr + ':' + mn)
elif h == 0 and m >= 60:
	print('01:0' + mn[1])
elif h == 0 and m < 60:
	print('01:' + mn)
elif h > 12 and m > 60:
	print('0' + hr[1] + ':0' + mn[1])
elif m >= 60:
	print(hr + ':0' + mn[1])
elif hr[1] != '0':
	print('0' + hr[1] + ':' + mn)
else:
	print('10:' + mn)


n = int(input())
s = list(input())
h = int(s[0])
h1 = int(s[1])
m = int(s[3])
m1 = int(s[4])
if n == 12:
	if h * 10 + h1 == 0:
		s[0] = '1'
	elif h * 10 + h1 > 12:
		if h > 1:
			if h1 == 0:
				s[0] = '1'
			else:
				s[0] = '0'
		else:
			s[1] = '1'
	if m * 10 + m1 > 59:
		s[3] = '5'
else:
	if h * 10 + h1 > 23:
		s[0] = '1'
	if m * 10 + m1 > 59:
		s[3] = '5'
print(''.join(s))


n = int(input())
s = input().split(':')
hh = s[0]
mm = s[1]
h = hh
m = mm
if n == 12:
	if int(hh) > 12 and int(hh) % 10 != 0:
		h = '0' + hh[1]
	elif int(hh) > 12:
		h = '10'
	if int(hh) == 0:
		h = '10'
	if int(mm) > 59:
		m = '0' + mm[1]
else:
	if int(hh) > 23:
		h = '0' + hh[1]
	if int(mm) > 59:
		m = '0' + mm[1]
print(h + ':' + m)


def doze(hora):
	novahora = ''
	if int(hora[0:2]) > 19:
		if int(hora[0:2]) % 10 == 0:
			novahora += '10'
		else:
			novahora += '0'
			novahora += hora[1]
	if int(hora[0:2]) > 12 and int(hora[0:2]) <= 19:
		novahora += hora[0]
		novahora += '0'
	if int(hora[0:2]) == 0:
		novahora += '01'
	if int(hora[0:2]) > 0 and int(hora[0:2]) <= 12:
		novahora += hora[0:2]
	novahora += ':'
	if int(hora[3:5]) > 59:
		novahora += '0'
		novahora += hora[4]
	else:
		novahora += hora[3:]
	return novahora

def vitequatro(hora):
	novahora = ''
	if int(hora[0:2]) > 23:
		novahora += '0'
		novahora += hora[1]
	else:
		novahora += hora[:2]
	novahora += ':'
	if int(hora[3:5]) > 59:
		novahora += '0'
		novahora += hora[4]
	else:
		novahora += hora[3:]
	return novahora
formato = int(input())
hora = input()
if formato == 24:
	print(vitequatro(hora))
else:
	print(doze(hora))


n = int(input())
x = list(input())
m = int(x[3]) * 10 + int(x[4])
h = int(x[0]) * 10 + int(x[1])
if m >= 60:
	x[3] = '0'
if n == 12:
	if h < 1:
		x[1] = '1'
	if h > 12:
		if h % 10 == 0:
			x[0] = '1'
		else:
			x[0] = '0'
if n == 24:
	if h > 23:
		x[0] = '1'
print(''.join(x))


n = int(input())
a = list(input())
for i in range(5):
	if i != 2:
		a[i] = int(a[i])
n1 = a[0] * 10 + a[1]
n2 = a[3] * 10 + a[4]
if n == 24:
	ans = 0
	if n1 >= 24:
		a[0] = 0
	if n2 >= 60:
		a[3] = 0
	print(''.join(map(str, a)))
if n == 12:
	if n2 >= 60:
		a[3] = 0
	if n1 == 0:
		a[0] = 1
	if n1 > 12:
		if a[1] == 0:
			a[0] = 1
		else:
			a[0] = 0
	print(''.join(map(str, a)))


form = int(input())
(h, m) = input().split(':')
(h1, m1) = map(int, (h, m))
if m[0] >= '6':
	m = '0' + m[1]
if form == 24:
	if h1 > 23:
		h = '0' + h[1]
if form == 12:
	if h1 == 0:
		h = '01'
	elif h1 > 12 and h1 % 10 == 0:
		h = '1' + h[1]
	elif h1 > 12:
		h = '0' + h[1]
print(h, m, sep=':')


n = int(input())
a = input()
x = int(a[:2])
y = int(a[3:])
if n == 12:
	if x == 0:
		a = '10' + a[2:]
	elif x > 12:
		if a[1] == '0':
			a = '1' + a[1:]
		else:
			a = '0' + a[1:]
	if y > 59:
		a = a[:3] + '0' + a[-1]
else:
	if x > 23:
		a = '0' + a[1:]
	if y > 59:
		a = a[:3] + '0' + a[-1]
print(a)


from __future__ import print_function

def solvecase():
	form = int(input().strip())
	(hh, mm) = map(int, input().strip().split(':'))
	if mm > 59:
		mm = mm % 10
	if form == 24:
		if hh > 23:
			hh = hh % 10
	elif form == 12:
		if hh > 12:
			if hh % 10:
				hh = hh % 10
			else:
				hh = 10
		if hh == 0:
			hh = 1
	return '%02d:%02d' % (hh, mm)

def main():
	print(solvecase())
main()


a = int(input())
b = list(input())
if a == 24:
	if int(b[0]) == 2:
		if int(b[1]) > 3:
			b[1] = '0'
	if int(b[0]) > 2:
		b[0] = '1'
	if int(b[3]) > 5:
		b[3] = '5'
if a == 12:
	if int(b[0]) == 1:
		if int(b[1]) > 2:
			b[1] = '0'
	if int(b[0]) > 1:
		if int(b[1]) > 0:
			b[0] = '0'
		if int(b[1]) == 0:
			b[0] = '1'
	if int(b[3]) > 5:
		b[3] = '5'
	if int(b[0]) == 0 and int(b[1]) == 0:
		b[1] = '1'
print(''.join(b))


f = int(input())
t = input()
if int(t[3:5]) >= 60:
	t = t[:3] + '0' + t[4]
if f == 12 and int(t[1]) <= 2 and (int(t[0]) > 1):
	t = '1' + t[1:]
if int(t[0:2]) > f or t[0:2] == '24':
	t = '0' + t[1:]
if f == 12 and int(t[0:2]) < 1:
	t = '01' + t[2:]
print(t)


format = int(input())
(h, m) = map(int, input().split(':'))
if m > 59:
	m %= 10
if format == 12:
	if h > 12:
		if h % 10 == 0:
			h = 10
		else:
			h %= 10
	elif h == 0:
		h = 1
elif format == 24:
	if h > 23:
		h %= 10
print(str(h // 10) + str(h % 10) + ':' + str(m // 10) + str(m % 10))


n = int(input())
s = input()
h = s[:2]
m = s[3:]
if n == 12:
	if 12 >= int(h) >= 1 and 59 >= int(m) >= 0:
		print(h + ':' + m)
	else:
		if int(m[0]) > 5:
			m = '0' + m[1]
		if h[0] == h[1] == '0':
			h = h[0] + '1'
		elif h[1] == '0':
			h = '1' + h[1]
		elif int(h) > 12:
			h = '0' + h[1]
		print(h + ':' + m)
elif 23 >= int(h) >= 0 and 59 >= int(m) >= 0:
	print(h + ':' + m)
else:
	if int(m[0]) > 5:
		m = '0' + m[1]
	if int(h) > 23:
		h = '0' + h[1]
	print(h + ':' + m)


n = int(input())
(s, s2) = input().split(':')
if n == 24:
	if s >= '24':
		s = '0' + s[1]
else:
	if s >= '13':
		s = '0' + s[1]
	if s == '00':
		s = '10'
if s2 >= '60':
	s2 = '1' + s2[1]
print(s + ':' + s2)


n = int(input())
s = [i for i in input()]
if n == 12:
	if int(s[0] + s[1]) == 0:
		s[1] = '1'
	elif int(s[0] + s[1]) > 12:
		if s[1] == '0':
			s[0] = '1'
		else:
			s[0] = '0'
elif int(s[0] + s[1]) > 23:
	s[0] = '0'
if int(s[3] + s[4]) > 59:
	s[3] = '0'
print(''.join(s))


n = input()
hour = input()
hour = list(hour)
if n == '24':
	if int(hour[0] + hour[1]) > 23:
		hour[0] = '0'
	if int(hour[3] + hour[4]) > 59:
		hour[3] = '0'
else:
	if int(hour[0] + hour[1]) > 12:
		hour[0] = '0'
	if int(hour[0] + hour[1]) == 0:
		hour[0] = '1'
	if int(hour[3] + hour[4]) > 59:
		hour[3] = '0'
print(''.join(hour))


I = input
R = range
R = R(1, 13) if I() == '12' else R(24)
a = I()
b = ':' + [a[3], '0'][int(a[3]) > 5] + a[4]
a = a[:2]
F = lambda x, y: (x[0] != y[0]) + (x[1] != y[1])
c = '10'
for i in R:
	s = '0' * (i <= 9) + str(i)
	if F(c, a) > F(s, a):
		c = s
print(c + b)


def confereHora(listah):
	hour = int(str(listah[0]) + str(listah[1]))
	minut = int(str(listah[2]) + str(listah[3]))
	if (hour >= 1 and hour <= 12) and (minut <= 59 and minut >= 0):
		return True
	return False

def confereHora2(listah):
	hour = int(str(listah[0]) + str(listah[1]))
	minut = int(str(listah[2]) + str(listah[3]))
	if (hour >= 0 and hour <= 23) and (minut <= 59 and minut >= 0):
		return True
	return False
f = int(input())
hora = list(map(str, input().split(':')))
lista = [int(hora[0][0]), int(hora[0][1]), int(hora[1][0]), int(hora[1][1])]
if f == 12:
	if not confereHora(lista):
		while True:
			if lista[0] > 1 and lista[1] < 3:
				lista[0] = 1
			if lista[0] == 1 and lista[1] > 2:
				lista[0] = 0
			if lista[1] == 0 and lista[0] == 0:
				lista[1] = 1
			if lista[0] > 1:
				lista[0] = 0
			if lista[2] > 5:
				lista[2] = 0
			if confereHora(lista):
				break
elif not confereHora2(lista):
	while True:
		if lista[0] > 2:
			lista[0] = 0
		if lista[0] == 2 and lista[1] > 3:
			lista[1] = 0
		if lista[2] > 5:
			lista[2] = 0
		if confereHora2(lista):
			break
hora = int(str(lista[0]) + str(lista[1]))
minut = int(str(lista[2]) + str(lista[3]))
print('{:02d}:{:02d}'.format(hora, minut))


f = input()
(h, m) = input().split(':')
if f == '12':
	if h == '00':
		h = '01'
	elif int(h) > 12:
		if h[1] == '0':
			h = '10'
		else:
			h = '0' + h[1]
if f == '24' and int(h) > 23:
	h = '1' + h[1]
if int(m) > 59:
	m = '3' + m[1]
print(h + ':' + m)


clock_type = input()
(hours, minutes) = input().split(':')
hours_list = list(map(int, hours))
minutes_list = list(map(int, minutes))
hours = int(hours)
minutes = int(minutes)
if clock_type == '12' and (hours > 12 or hours == 0):
	if hours_list[0] == 0 and hours_list[1] == 0:
		hours_list[0] = 1
	if hours_list[1] == 0:
		hours_list[0] = 1
	else:
		hours_list[0] = 0
if clock_type == '24' and hours > 23:
	hours_list[0] = 0
if minutes > 59:
	minutes_list[0] = 0
hh = str(hours_list[0]) + str(hours_list[1])
mm = str(minutes_list[0]) + str(minutes_list[1])
print(hh + ':' + mm)


import logging
import copy
import sys
import math
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)

def solve(formatStr, timeStr):
	if int(timeStr[3]) > 5:
		timeStr = timeStr[:3] + '0' + timeStr[4:]
	if formatStr == 24:
		if int(timeStr[0]) > 2:
			timeStr = '0' + timeStr[1:]
		elif int(timeStr[0]) == 2 and int(timeStr[1]) >= 4:
			timeStr = timeStr[0] + '0' + timeStr[2:]
	if formatStr == 12:
		if int(timeStr[0]) > 1:
			if int(timeStr[1]) == 0:
				timeStr = '1' + timeStr[1:]
			else:
				timeStr = '0' + timeStr[1:]
		elif int(timeStr[0]) == 1 and int(timeStr[1]) > 2:
			timeStr = '0' + timeStr[1:]
		elif int(timeStr[0]) == 0 and int(timeStr[1]) == 0:
			timeStr = '01' + timeStr[2:]
	return timeStr

def main():
	formatStr = int(input())
	print(solve(formatStr, input()))

def log(*message):
	logging.debug(message)
main()


mode = int(input())
time = input()
h = time[:2]
m = time[3:]
if m[0] > '5':
	m1 = '0' + m[1]
else:
	m1 = m
if mode == 24:
	if h > '23':
		h1 = '0' + h[1]
	else:
		h1 = h
	print(h1 + ':' + m1)
else:
	if h > '12':
		if h[1] < '3':
			h1 = '1' + h[1]
		else:
			h1 = '0' + h[1]
	elif h == '00':
		h1 = '01'
	else:
		h1 = h
	print(h1 + ':' + m1)


def getHour(num, horas, minutos):
	dezHoras = int(horas[0])
	uniHoras = int(horas[1])
	dezMin = int(minutos[0])
	uniMin = int(minutos[1])
	if num == 12:
		if dezHoras * 10 + uniHoras > 12 and uniHoras != 0:
			dezHoras = 0
		elif dezHoras * 10 + uniHoras > 12 and uniHoras == 0:
			dezHoras = 1
		elif dezHoras * 10 + uniHoras == 0:
			uniHoras = 1
		if dezMin >= 6:
			dezMin = 0
		return '{}{}:{}{}'.format(str(dezHoras), str(uniHoras), str(dezMin), str(uniMin))
	else:
		if dezHoras * 10 + uniHoras >= 24:
			dezHoras = 0
		if dezMin >= 6:
			dezMin = 0
		return '{}{}:{}{}'.format(str(dezHoras), str(uniHoras), str(dezMin), str(uniMin))
num = int(input())
entrada = input().split(':')
horas = entrada[0]
minutos = entrada[1]
print(getHour(num, horas, minutos))


n = int(input())
(a, b) = map(int, input().split(':'))
while b > 59:
	b -= 10
if 12 == n:
	while a <= 0:
		a += 10
	while a > 12:
		a -= 10
if 24 == n:
	while a >= 24:
		a -= 10
print('%02d:%02d' % (a, b))


def main():
	s = input() == '12'
	(h, m) = map(int, input().split(':'))
	if not 0 <= h - s < (2 - s) * 12:
		h = (h - s) % ((2 - s) * 10) + s
	print('{:0>2d}:{:0>2d}'.format(h, m % 60))
main()


def main():
	n = int(input())
	s = input()
	(a, b) = s.split(':')
	if int(b) > 59:
		b = '0' + b[1]
	if n == 12:
		if int(a) > 12 and a[1] != '0':
			a = '0' + a[1]
		elif int(a) > 12 and a[1] == '0':
			a = '1' + a[1]
		if int(a) == 0:
			a = '01'
	elif int(a) > 23:
		a = '0' + a[1]
	print(a + ':' + b)
main()


form = int(input())
ti = list(map(int, input().split(':')))
if form == 12:
	if ti[-1] > 59:
		ti[-1] = 10 + ti[-1] % 10
	if ti[0] % 10 == 0 and ti[0] != 10:
		ti[0] = 10
	elif ti[0] > 12:
		ti[0] = ti[0] % 10
else:
	if ti[-1] > 59:
		ti[-1] = 10 + ti[-1] % 10
	if ti[0] % 10 == 0 and ti[0] != 10 and (ti[0] != 20) and (ti[0] != 0):
		ti[0] = 10
	elif ti[0] > 23:
		ti[0] = ti[0] % 10
l = [str(i) for i in ti]
if len(l[0]) < 2:
	print(0, end='')
print(l[0], end=':')
if len(l[1]) < 2:
	print(0, end='')
print(l[1], end='')


n = int(input())
s = str(input())
h = s[0:2]
m = s[3:]
if n == 24:
	if int(h) >= 24:
		h = '0' + h[1]
elif int(h) <= 12 and int(h) != 0:
	b = 0
elif int(h) == 0:
	h = '01'
elif h[1] == '0':
	h = '10'
else:
	h = '0' + h[1]
if int(m) <= 59:
	b = 0
else:
	m = '0' + m[1]
print(h + ':' + m)


n = int(input())
S = input()
S1 = ''
if n == 12:
	if S[0] > '1':
		if S[1] != '0':
			S1 = S1 + '0'
		else:
			S1 = S1 + '1'
	elif S[0] != '0' or S[1] != '0':
		S1 = S1 + S[0]
	if S[1] > '2' and S1[0] > '0':
		S1 = S1 + '1'
	elif S[0] != '0' or S[1] != '0':
		S1 = S1 + S[1]
	if S[0] == '0' and S[1] == '0':
		if S[0] == '0' and S[1] == '0':
			S1 = '01'
		else:
			S1 = '10'
	S1 = S1 + ':'
	if S[3] > '5':
		S1 = S1 + '0'
	else:
		S1 = S1 + S[3]
	S1 = S1 + S[4]
if n == 24:
	if S[0] > '2':
		S1 = S1 + '0'
	else:
		S1 = S1 + S[0]
	if S[0] == '2':
		if S[1] > '3':
			S1 = S1 + '0'
		else:
			S1 = S1 + S[1]
	else:
		S1 = S1 + S[1]
	S1 = S1 + ':'
	if S[3] > '5':
		S1 = S1 + '0'
	else:
		S1 = S1 + S[3]
	S1 = S1 + S[4]
print(S1)


n = int(input())
time = input()
numbers = time.split(':')
(h, m) = (numbers[0], numbers[1])
(a, b) = (int(h), int(m))
if n == 12:
	if a > 12:
		if a % 10 == 0:
			h = '1' + h[1]
		else:
			h = '0' + h[1]
	elif a == 0:
		h = '10'
elif a >= 24:
	h = '0' + h[1]
if b >= 60:
	m = '0' + m[1]
print('%s:%s' % (h, m))


def fn1():
	time = list(input())
	if int(time[0]) > 1:
		if int(time[1]) < 2:
			time[0] = '1'
		else:
			time[0] = '0'
	if time[0] != '0':
		if int(time[1]) > 2:
			time[1] = '1'
	if int(time[3]) >= 6:
		time[3] = '5'
	if time[0] == time[1] == '0':
		time[1] = '1'
	print(''.join(time))
	return

def fn2():
	time = list(input())
	if int(time[0]) > 2:
		time[0] = '0'
	if int(time[0]) >= 2:
		if int(time[1]) >= 4:
			time[1] = '3'
	if int(time[3]) >= 6:
		time[3] = '5'
	print(''.join(time))
	return
f = int(input())
if f == 12:
	fn1()
else:
	fn2()


fmt = int(input())
(h, m) = map(int, input().split(':'))
m %= 60
if fmt == 12:
	if h == 0:
		h = 1
	elif h > 12:
		if h % 10:
			h %= 10
		else:
			h = 10
elif h > 23:
	h %= 10
print('{:0>2}:{:0>2}'.format(h, m))


formato = int(input())
horario = input().split(':')

def monta_string(formato, lista):
	saida = ''
	if formato == 24:
		if int(lista[0]) >= formato:
			saida += '1'
			saida += lista[0][1]
		else:
			saida += lista[0]
	if formato == 12:
		if int(lista[0]) > formato:
			saida += '0'
			saida += lista[0][1]
		else:
			saida += lista[0]
		if int(saida) == 0:
			saida = '10'
	saida += ':'
	if int(lista[1]) > 59:
		saida += '0'
		saida += lista[1][1]
	else:
		saida += lista[1]
	return saida
print(monta_string(formato, horario))


__author__ = 'Think'
type = input()
time = input()
hours = time[0:2]
minutes = time[3:]
if type == '12':
	if hours[0] == '0':
		if hours[1] != '0':
			print(hours, end='')
		else:
			print('01', end='')
	elif hours[0] == '1':
		if hours[1] in ['0', '1', '2']:
			print(hours, end='')
		else:
			print('10', end='')
	elif hours[1] != '0':
		print('0' + hours[1], end='')
	else:
		print('10', end='')
elif hours[0] in ['0', '1']:
	print(hours, end='')
elif hours[0] == '2' and hours[1] in ['0', '1', '2', '3']:
	print(hours, end='')
else:
	print('0' + hours[1], end='')
print(':', end='')
if int(minutes[0]) <= 5:
	print(minutes)
else:
	print('0' + minutes[1])


f = int(input())
(h, m) = map(str, input().split(':'))
if m > '59':
	m = '0' + m[1]
if f == 24:
	if h > '23':
		h = '1' + h[1]
elif f == 12:
	if h > '12':
		h = '0' + h[1]
	if h == '00':
		h = '10'
print(h + ':' + m)


n = input()[0]
(h, m) = map(int, input().split(':'))
if 23 < h or ('2' > n and (not 0 < h < 13)):
	h = 1 + (9 + h) % 10
if 59 < m:
	m -= 40
print('{:02}:{:02}'.format(h, m))


tipe = int(input())
time = input()
if tipe == 12:
	if int(time[:2]) > 12 and int(time[:2]) % 10 != 0:
		time = '0' + time[1:]
	elif int(time[:2]) % 10 == 0:
		time = '1' + time[1:]
elif int(time[:2]) > 23:
	time = '1' + time[1:]
if int(time[3:]) > 59:
	time = time[:3] + '0' + time[-1]
print(time)


__author__ = 'artyom'

def main():
	s = read(0)
	t = read(0).split(':')
	if s == '12':
		if t[0][1] == '0':
			t[0] = '10'
		elif int(t[0]) > 12:
			t[0] = '0' + t[0][1]
	elif int(t[0]) > 23:
		t[0] = '0' + t[0][1]
	if int(t[1]) > 59:
		t[1] = '0' + t[1][1]
	return t[0] + ':' + t[1]

def read(mode=1, size=None):
	if mode == 0:
		return input().strip()
	if mode == 1:
		return int(input().strip())
	if mode == 2:
		return input().strip().split()
	if mode == 3:
		return list(map(int, input().strip().split()))
	a = []
	for _ in range(size):
		a.append(read(3))
	return a

def write(s='\n'):
	if s is None:
		s = ''
	if isinstance(s, tuple) or isinstance(s, list):
		s = ' '.join(map(str, s))
	s = str(s)
	print(s, end='\n')
write(main())


a = int(input())
b = input()
c = b[0:2]
d = b[3:5]
if a == 12:
	if c[0] == '0':
		if c[1] == '0':
			c = c[0] + '1'
	elif c[0] == '1':
		if int(c[1]) > 2:
			c = c[0] + '1'
	elif c[1] == '0':
		c = '1' + c[1]
	else:
		c = '0' + c[1]
elif a == 24:
	if int(c[0]) > 2:
		c = '0' + c[1]
	elif c[0] == '2':
		if int(c[1]) > 3:
			c = c[0] + '0'
if int(d[0]) > 5:
	d = '0' + d[1]
print(c + ':' + d)


form = int(input())
(h, m) = map(str, input().split(':'))
h = list(h)
m = list(m)
changes = 0
if int(m[0]) > 5:
	changes += 1
	m[0] = '1'
if form == 24:
	if int(h[0]) > 2:
		h[0] = '0'
		changes += 1
	elif int(h[0]) == 2:
		if int(h[1]) > 3:
			h[1] = '3'
else:
	if int(h[0]) > 1:
		h[0] = '0'
	elif h[0] == '1':
		if int(h[1]) > 2:
			h[0] = '0'
	if h[0] == '0':
		if h[1] == '0':
			h[0] = '1'
print(h[0] + h[1], m[0] + m[1], sep=':')


t = input()
time = input().split(':')
h = int(time[0])
m = int(time[1])
if m >= 60:
	m = m % 10
if t == '12':
	if h >= 13 or h == 0:
		if h % 10 == 0:
			h = 10
		else:
			h = h % 10
elif h >= 24:
	h = h % 10
print('%02d:%02d' % (h, m))


import sys
import math
import bisect

def query_distance(s, t):
	n = len(s)
	ans = 0
	for i in range(n):
		if s[i] != t[i]:
			ans += 1
	return ans

def solve(n, s):
	min_val = 10 ** 18
	sol = None
	if n == 24:
		for i in range(0, 24):
			for j in range(0, 60):
				t = str(i).zfill(2) + ':' + str(j).zfill(2)
				val = query_distance(s, t)
				if min_val > val:
					min_val = val
					sol = t
	elif n == 12:
		for i in range(1, 13):
			for j in range(0, 60):
				t = str(i).zfill(2) + ':' + str(j).zfill(2)
				val = query_distance(s, t)
				if min_val > val:
					min_val = val
					sol = t
	return sol

def main():
	n = int(input())
	s = input()
	ans = solve(n, s)
	print(ans)
main()


clock_type = int(input())
(h, m) = map(int, input().split(':'))
if m >= 60:
	m -= 60
if clock_type == 24:
	if h >= 24:
		h = 10 + h % 10
else:
	if h == 0:
		h = 1
	if h > 12:
		h = h % 10
		if h == 0:
			h = 10
ans = ''
if h < 10:
	ans += '0'
ans += str(h) + ':'
if m < 10:
	ans += '0'
ans += str(m)
print(ans)


n = int(input())
s = input()
(h, m) = [int(i) for i in s.split(':')]
u = h
if m not in range(0, 60):
	m = m % 10
if m in range(0, 10):
	m = '0' + str(m)
if h in range(0, 10):
	h = '0' + str(h)
else:
	h = str(h)
if n == 12:
	if u not in range(1, 13):
		h = list(h)
		if h[0] == '0':
			h[1] = '1'
		elif h[0] == '1':
			h[1] = '2'
		elif h[1] == '0':
			h[0] = '1'
		else:
			h[0] = '0'
		h = ''.join(h)
elif u not in range(0, 24):
	h = list(h)
	if h[0] == '2':
		h[1] = '2'
	else:
		h[0] = '1'
	h = ''.join(h)
print('{}:{}'.format(h, m))


form = int(input())
time = input()
if form == 24:
	if int(time[0:2]) >= 24:
		time = '0' + time[1:5]
	if int(time[3:5]) >= 60:
		time = time[0:3] + '0' + time[4]
elif form == 12:
	if int(time[0:2]) > 12 and (not int(time[1]) == 0):
		time = '0' + time[1:]
	elif int(time[0:2]) > 12 and int(time[1]) == 0:
		time = '1' + time[1:]
	if int(time[0:2]) == 0:
		time = time[0] + '7' + time[2:]
	if int(time[3:5]) >= 60:
		time = time[0:3] + '0' + time[4]
print(time)


def l(s1, s2):
	return len(list((i for i in range(len(s1)) if s1[i] != s2[i])))
c = int(input())
if c == 12:
	h = list(range(1, 13))
else:
	h = list(range(0, 24))
s = input()
min_s = ''
min_l = 100
for x in h:
	for y in range(60):
		new_s = '{0:02}:{1:02}'.format(x, y)
		if l(s, new_s) < min_l:
			min_l = l(s, new_s)
			min_s = new_s
print(min_s)


n = int(input())
s = input()
hh = s[:2]
mm = s[3:]
if n == 24:
	th = int(hh)
	if th >= 24:
		hh = '0' + hh[1]
	tm = int(mm)
	if tm > 59:
		mm = '0' + mm[1]
else:
	th = int(hh)
	if th > 12:
		if th % 10 == 0:
			hh = '1' + hh[1]
		else:
			hh = '0' + hh[1]
	if hh == '00':
		hh = '10'
	tm = int(mm)
	if tm > 59:
		mm = '0' + mm[1]
s = hh + ':' + mm
print(s)


f = input()
t = input()
l = list(t)
if int(l[3]) >= 6:
	l[3] = '0'
if f == '12':
	if int(l[0]) >= 2:
		if l[1] != '0':
			l[0] = '0'
		else:
			l[0] = '1'
	elif l[0] == '0' and l[1] == '0':
		l[0] = '1'
	elif l[0] == '1' and int(l[1]) >= 3:
		l[0] = '0'
if f == '24':
	if int(l[0]) >= 3:
		l[0] = '0'
	elif l[0] == '2' and int(l[1]) >= 4:
		l[0] = '0'
print(''.join(l))


clockFormat = input()
brokenTime = input()
parts = brokenTime.split(':')
hours = int(parts[0])
minutes = int(parts[1])
if minutes > 59:
	minutes = '0' + brokenTime[-1:]
else:
	minutes = parts[1]
if clockFormat == '12':
	if hours == 0:
		hours = '01'
	elif hours > 12:
		if parts[0][1:2] == '0':
			hours = '10'
		else:
			hours = '0' + brokenTime[1:2]
	else:
		hours = parts[0]
elif hours > 23:
	hours = '0' + brokenTime[1:2]
else:
	hours = parts[0]
print(str(hours) + ':' + str(minutes))


a = int(input())
b = input().replace('', ' ').split()
b.remove(':')
b = [*map(int, b)]
if a == 12:
	if b[0] == 0 and b[1] == 0:
		b[1] = 1
	elif b[0] > 1 and b[1] > 2:
		b[0] = 0
	elif b[0] == 1 and b[1] > 2:
		b[0] = 0
	elif b[0] > 1 and b[1] == 0:
		b[0] = 1
	elif b[0] > 1:
		b[0] = 0
	if b[2] > 5:
		b[2] = 0
else:
	if b[2] > 5:
		b[2] = 0
	if b[0] > 2:
		b[0] = 0
	elif b[0] == 2 and b[1] >= 4:
		b[0] = 0
print(''.join(map(str, b[:2])) + ':' + ''.join(map(str, b[2:])))


format = input()
time = list(input().split(':'))
if format == '12':
	if time[0] == '00':
		time[0] = '01'
	elif int(time[0]) > 12:
		r = '0'
		if int(time[0]) % 10 == 0:
			r = '1'
		time[0] = r + time[0][-1]
elif format == '24':
	if int(time[0]) > 23:
		time[0] = '1' + time[0][-1]
if int(time[1]) > 59:
	time[1] = '1' + time[1][-1]
print(time[0] + ':' + time[1])


t = input()
s = input().split(':')
hh = s[0]
mm = s[1]
if int(hh) > 23:
	hh = '0' + hh[1]
if int(mm) > 59:
	mm = '0' + mm[1]
if t == '24':
	print(hh + ':' + mm)
	exit()
if int(hh) > 12:
	hh = '0' + hh[1]
if int(hh) == 0:
	hh = '1' + hh[1]
print(hh + ':' + mm)


form = input()
(h, m) = input().split(':')
out = ''
if form == '12':
	if int(h) > 12:
		if h[1] == '0':
			out += '%d0:' % 1
		else:
			out += '0%s:' % h[1]
	elif h == '00':
		out += '01:'
	else:
		out += h + ':'
	if int(m) > 59:
		out += '0%s' % m[1]
	else:
		out += m
else:
	if int(h) > 23:
		out += '0%s:' % h[1]
	else:
		out += h + ':'
	if int(m) > 59:
		out += '0%s' % m[1]
	else:
		out += m
print(out)


system = int(input())
r = [c for c in input()]
if int(r[3]) > 5:
	r[3] = '0'
if system == 12 and int(r[1]) != 0 and (int(r[0]) > 2):
	r[0] = '0'
elif system == 12 and (int(r[0]) > 1 and int(r[1]) != 0 or (int(r[0]) == 1 and int(r[1]) > 2)):
	r[0] = '0'
elif system == 24 and (int(r[0]) > 2 or (int(r[0]) == 2 and int(r[1]) > 3)):
	r[0] = '0'
elif system == 12 and (int(r[0]) > 1 and int(r[1]) == 0):
	r[0] = '1'
if system == 12 and (int(r[0]) > 1 or r[0] == '0') and (r[1] == '0'):
	r[1] = '1'
print(''.join(r))


n = int(input())
a = list(input())
if n == 24:
	if a[0] > '2':
		a[0] = '0'
	elif a[0] == '2' and a[1] > '3':
		a[0] = '0'
elif a[0] > '1':
	if a[1] == '0':
		a[0] = '1'
	else:
		a[0] = '0'
elif a[0] == '1' and a[1] > '2':
	a[0] = '0'
elif a[0] == '0' and a[1] == '0':
	a[1] = '1'
if a[3] > '5':
	a[3] = '0'
print(''.join(a))


a = int(input())
s = input()
c = 0
ans = []
for i in range(0, len(s)):
	ans.append(s[i])
if s[3] > '5':
	ans[3] = 0
if a == 12:
	if s[0] == '0' and s[1] == '0':
		ans[1] = '1'
	if s[0] == '1' and s[1] > '2':
		ans[1] = '2'
	if s[0] > '1' and s[1] == '0':
		ans[0] = '1'
	if s[0] > '1' and s[1] != '0':
		ans[0] = '0'
else:
	if s[0] == '2' and s[1] > '3':
		ans[1] = '3'
	if s[0] > '2':
		ans[0] = '0'
for ii in ans:
	print(ii, end='')


def broken_clock():
	form = int(input())
	time = input().split(':')
	if form == 12 and int(time[0]) not in range(1, 13):
		if time[0][1] == '0':
			time[0] = '1' + time[0][1]
		else:
			time[0] = '0' + time[0][1]
	elif form == 24 and int(time[0]) not in range(0, 24):
		if time[0] == '24':
			time[0] = '23'
		else:
			time[0] = '0' + time[0][1]
	if int(time[1]) > 59:
		time[1] = '1' + time[1][1]
	return time[0] + ':' + time[1]
print(broken_clock())


n = int(input())
s = str(input())
h1 = int(s[0])
h2 = int(s[1])
m1 = int(s[3])
m2 = int(s[4])
hh = h1 * 10 + h2
mm = m1 * 10 + m2
l1 = ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']
l2 = ['00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23']
if mm > 59:
	m1 = 1
if n == 12:
	ma = 2
	s1 = s[:2]
	for i in range(12):
		x = 0
		if s[0] != l1[i][0]:
			x += 1
		if s[1] != l1[i][1]:
			x += 1
		if x < ma:
			ma = x
			s1 = l1[i]
else:
	ma = 2
	s1 = s[:2]
	for i in range(24):
		x = 0
		if s[0] != l2[i][0]:
			x += 1
		if s[1] != l2[i][1]:
			x += 1
		if x < ma:
			ma = x
			s1 = l2[i]
print(s1 + ':' + str(m1) + str(m2))


def doze(tempo):
	retorno = ''
	if int(tempo[0:2]) == 0:
		tempo = tempo[:1] + '1' + tempo[2:]
	if int(tempo[0]) > 1:
		tempo = '1' + tempo[1:3] + tempo[3:]
	if int(tempo[0:2]) < 12:
		if int(tempo[3:]) < 60:
			retorno = tempo
			return tempo
	if int(tempo[0:2]) > 12:
		tempo = '0' + tempo[1:3] + tempo[3:]
	if int(tempo[3:]) >= 60:
		tempo = tempo[:3] + '0' + tempo[4:]
	retorno = tempo
	return retorno

def vinte_e_quatro(tempo):
	retorno = ''
	if int(tempo[0:2]) < 24:
		if int(tempo[3:]) < 60:
			retorno = tempo
			return retorno
	if int(tempo[0:2]) > 23:
		tempo = '0' + tempo[1:3] + tempo[3:]
	if int(tempo[3:]) >= 60:
		tempo = tempo[:3] + '0' + tempo[4:]
	retorno = tempo
	return retorno
formato_horas = int(input())
tempo_atual = str(input())
if formato_horas == 12:
	print(doze(tempo_atual))
else:
	print(vinte_e_quatro(tempo_atual))


form = input()
(h, m) = input().split(':')
h = int(h)
m = int(m)
if m > 59:
	m %= 10
if form == '24':
	if h > 23:
		h %= 10
if form == '12':
	if h == 0:
		h = 1
	if h > 12:
		if h % 10 == 0:
			h = 10
		else:
			h %= 10
if h < 10:
	h = '0' + str(h)
else:
	h = str(h)
if m < 10:
	m = '0' + str(m)
else:
	m = str(m)
print(h + ':' + m)


n = int(input())
(a, b) = map(int, input().split(':'))
if b > 59:
	b = '0' + str(b)[-1]
if a > n or a == 24:
	a = '0' + str(a)[-1]
if n == 12 and int(a) % 10 == 0:
	a = '1' + str(a)[-1]
	if int(a) > 12:
		a = '10'
if a == 0 and n == 12:
	a = '01'
(a, b) = (str(a), str(b))
print(a.zfill(2), b.zfill(2), sep=':')


fmt = input()
(h, m) = [int(i) for i in input().split(':')]
h_min = 0 if fmt == '24' else 1
h_max = 23 if fmt == '24' else 12
while not h_min <= h <= h_max:
	if h == 0:
		h = 1
	else:
		h -= 10
while not 0 <= m <= 59:
	m -= 10
print(('' if h >= 10 else '0') + str(h), ('' if m >= 10 else '0') + str(m), sep=':')


n = int(input())
t = list(map(list, input().split(':')))
if int(''.join(t[1])) >= 60:
	t[1][0] = '0'
if int(''.join(t[0])) >= 24 and n == 24:
	t[0][0] = '0'
if (int(''.join(t[0])) > 12 or int(''.join(t[0])) == 0) and n == 12:
	if int(t[0][1]) != 0:
		t[0][0] = '0'
	else:
		t[0][0] = '1'
print(''.join(t[0]) + ':' + ''.join(t[1]))


x = int(input())
ch = input().split(':')
h = int(ch[0])
m = int(ch[1])
res = ''
if x == 12:
	if h > 12:
		if ch[0][1] == '0':
			res = '10'
		else:
			res = '0' + ch[0][1]
	elif h < 1:
		res = '01'
	else:
		res = ch[0]
elif h > 23:
	if ch[0][1] == '0':
		res = '10'
	else:
		res = '0' + ch[0][1]
elif h < 0:
	None
else:
	res = ch[0]
if m > 59:
	if ch[1][1] == '0':
		res = res + ':' + '10'
	else:
		res = res + ':' + '0' + ch[1][1]
else:
	res = res + ':' + ch[1]
print(res)


x = int(input())
(a, b) = map(str, input().split(':'))
ct = 0
if x == 12:
	if a in ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12']:
		ct += 0
	else:
		ct += 1
		if a[1] == '0':
			a = '10'
		else:
			a = '0' + a[1]
if x == 24:
	if a in ['01', '02', '03', '04', '05', '06', '07', '08', '09'] + [str(i) for i in range(10, 24)]:
		ct = ct
	else:
		ct += 1
		a = '0' + a[1]
if b in ['01', '02', '03', '04', '05', '06', '07', '08', '09'] + [str(i) for i in range(10, 60)]:
	ct = ct
else:
	ct += 1
	b = '0' + b[1]
print(a + ':' + b)


a = input()
word = input()
words = word.split(':')
first = int(words[0])
second = int(words[1])
if a == '12' and words[0] == '00':
	words[0] = '01'
if a == '24' and first > 23:
	words[0] = '0' + words[0][1]
elif a == '12' and first > 12:
	words[0] = '0' + words[0][1]
if a == '12' and words[0] == '00':
	words[0] = '10'
if second > 59:
	words[1] = '0' + words[1][1]
print(f'{words[0]}:{words[1]}')


w = int(input(''))
q = input('')
qe = q[0]
qr = q[1]
qq = qe + qr
wq = q[3]
we = q[4]
ww = wq + we
if w == 12:
	qq = int(qq)
	ww = int(ww)
	qe = int(qe)
	if qq == 0:
		qr = '1'
	if qq > 12:
		if qe > 0:
			qq = '1' + qr
			qq = int(qq)
		if qq > 12:
			qq = '0' + qr
	else:
		qe = str(qe)
		qq = qe + qr
else:
	qq = int(qq)
	ww = int(ww)
	if qq > 23:
		qq = '0' + qr
	else:
		qq = qe + qr
if ww >= 60:
	ww = '0' + we
else:
	ww = wq + we
qq = str(qq)
print(qq + ':' + ww)


time = int(input())
x = input()
a = int(x[0])
b = int(x[1])
c = int(x[3])
d = int(x[4])
if time == 12:
	if a == 0 and b == 0:
		b = 1
	elif a > 1 and b > 0:
		a = 0
	elif a > 1 and b == 0:
		a = 1
	elif a == 1 and b > 2:
		b = 0
	if c > 5:
		c = 0
else:
	if a > 2:
		a = 0
	elif a == 2 and b > 3:
		b = 0
	if c > 5:
		c = 0
print('{}{}:{}{}'.format(a, b, c, d))


n = int(input())
s = input()
num = 5
ans = ''
for k1 in range(3):
	for k2 in range(10):
		for k3 in range(10):
			for k4 in range(10):
				tmp = 0
				if n == 24:
					if 0 <= k1 < 2 and 0 <= k2 < 10 and (0 <= k3 < 6) and (0 <= k4 < 10):
						tmp += int(k1 != int(s[0])) + int(k2 != int(s[1])) + int(k3 != int(s[3])) + int(k4 != int(s[4]))
						if tmp < num:
							num = tmp
							ans = str(k1) + str(k2) + ':' + str(k3) + str(k4)
					elif k1 == 2 and 0 <= k2 < 4 and (0 <= k3 < 6) and (0 <= k4 < 10):
						tmp += int(k1 != int(s[0])) + int(k2 != int(s[1])) + int(k3 != int(s[3])) + int(k4 != int(s[4]))
						if tmp < num:
							num = tmp
							ans = str(k1) + str(k2) + ':' + str(k3) + str(k4)
				elif k1 == 0 and 0 < k2 < 10 and (0 <= k3 < 6) and (0 <= k4 < 10):
					tmp += int(k1 != int(s[0])) + int(k2 != int(s[1])) + int(k3 != int(s[3])) + int(k4 != int(s[4]))
					if tmp < num:
						num = tmp
						ans = str(k1) + str(k2) + ':' + str(k3) + str(k4)
				elif k1 == 1 and 0 <= k2 <= 2 and (0 <= k3 < 6) and (0 <= k4 < 10):
					tmp += int(k1 != int(s[0])) + int(k2 != int(s[1])) + int(k3 != int(s[3])) + int(k4 != int(s[4]))
					if tmp < num:
						num = tmp
						ans = str(k1) + str(k2) + ':' + str(k3) + str(k4)
print(ans)


def find(vmin, vmax, ref):
	best = 3
	besti = []
	for a in range(0, 10):
		for b in range(0, 10):
			cur = a * 10 + b
			if not (vmin <= cur and cur <= vmax):
				continue
			cnt = 0
			if a != ref[0]:
				cnt += 1
			if b != ref[1]:
				cnt += 1
			if cnt < best:
				best = cnt
				besti = cur
	res = []
	res.append(int(besti / 10))
	res.append(besti % 10)
	return res
op = input()
s = input()
hh = list(map(int, list(s[0:2])))
mm = list(map(int, list(s[3:5])))
hh1 = []
mm1 = []
if op == '12':
	hh1 = find(1, 12, hh)
	mm1 = find(0, 59, mm)
else:
	hh1 = find(0, 23, hh)
	mm1 = find(0, 59, mm)
hs = ''.join(map(str, hh1))
ms = ''.join(map(str, mm1))
res = hs + ':' + ms
print(res)


def get(x):
	if x < 10:
		return '0' + str(x)
	return str(x)

def diff(a, b):
	ans = 0
	for i in range(len(a)):
		if a[i] != b[i]:
			ans += 1
	return ans
x = int(input())
s = input()
if x == 12:
	best = '01:30'
	for a in range(1, 13):
		for b in range(60):
			h = get(a) + ':' + get(b)
			if diff(s, h) < diff(best, s):
				best = h
	print(best)
else:
	best = '01:30'
	for a in range(24):
		for b in range(60):
			h = get(a) + ':' + get(b)
			if diff(s, h) < diff(best, s):
				best = h
	print(best)


format = input()
time = input()
(hours, minutes) = time.split(':')
ihours = int(hours)
if int(minutes) > 59:
	minutes = '3' + minutes[1]
if format == '12':
	maxf = 12
	minf = 1
else:
	maxf = 23
	minf = 0
if maxf < int(hours):
	hours = '0' + hours[1]
if int(hours) < minf:
	hours = '1' + hours[1]
print(hours + ':' + minutes)


t = int(input())
s = input().split(':')
r = s[0][0]
t1 = s[0][1]
if t == 12:
	if int(s[0]) == 0:
		t1 = '1'
	if int(s[0]) <= 12:
		if int(s[1]) < 60:
			print(r + t1 + ':' + s[1])
		else:
			print(r + t1 + ':0' + s[1][1])
	elif int(s[1]) < 60:
		if t1 != '0':
			print('0' + t1 + ':' + s[1])
		else:
			print('1' + t1 + ':' + s[1])
	elif t1 != 0:
		print('0' + t1 + ':0' + s[1][1])
	else:
		print('1' + t1 + ':0' + s[1][1])
elif int(s[0]) < 24:
	if int(s[1]) < 60:
		print(s[0] + ':' + s[1])
	else:
		print(s[0] + ':0' + s[1][1])
elif int(s[1]) < 60:
	print('0' + s[0][1] + ':' + s[1])
else:
	print('0' + s[0][1] + ':0' + s[1][1])


n = int(input())
(x, y) = map(int, input().split(':'))
if y > 59:
	y = y % 10
if n == 12:
	if x > 12:
		if x % 10 == 0:
			x = 10
		else:
			x %= 10
	elif x == 0:
		x = 1
elif n == 24:
	if x > 23:
		x %= 10
print(str(x // 10) + str(x % 10) + ':' + str(y // 10) + str(y % 10))


f = input()
(h, m) = input().split(':')
if int(m) > 59:
	m = '0' + m[1]
if f == '12' and (not 0 < int(h) < 13):
	h = '0' + h[1] if int(h[1]) else '10'
if f == '24' and (not 0 <= int(h) < 24):
	h = '0' + h[1] if int(h[1]) else '10'
print('{:0>2}:{:0>2}'.format(h, m))


n = int(input())
(hh, mm) = [int(i) for i in input().split(':')]
if mm > 59:
	mm %= 10
if n == 24:
	if hh > 23:
		hh %= 10
elif hh > 12 and hh % 10 != 0:
	hh %= 10
elif hh > 12 and hh % 10 == 0:
	hh = 10
elif hh == 0:
	hh = 1
hh = str(hh)
mm = str(mm)
print(('0' if len(hh) == 1 else '') + hh + ':' + ('0' if len(mm) == 1 else '') + mm)


n = int(input())
s = input().split(':')
if n == 12:
	c_t = list()
	for (i, n_s) in enumerate(s):
		if i == 0:
			s = ''
			s += '0' if n_s[0] > '1' and n_s[1] != '0' else '1' if n_s[1] == '0' else n_s[0]
			s += '1' if n_s[1] > '2' and s[0] == '1' or (n_s[1] == '0' and s[0] == '0') else n_s[1]
			c_t.append(s)
		else:
			c_t.append(('0' if n_s[0] > '5' else n_s[0]) + n_s[1])
	print(':'.join(c_t))
else:
	c_t = list()
	for (i, n_s) in enumerate(s):
		if i == 0:
			c_t.append(('0' if n_s[0] > '2' else n_s[0]) + ('1' if n_s[0] == '2' and n_s[1] > '3' else n_s[1]))
		else:
			c_t.append(('0' if n_s[0] > '5' else n_s[0]) + n_s[1])
	print(':'.join(c_t))


f = int(input())
(h, m) = map(list, input().split(':'))
h = list(map(int, h))
m = list(map(int, m))
if f == 12:
	if h[0] == 0 and h[1] == 0:
		h[1] = 1
	elif h[0] == 1:
		if h[1] > 2:
			h[1] = 0
	elif h[0] > 1:
		if h[1] == 0:
			h[0] = 1
		else:
			h[0] = 0
if f == 24:
	if h[0] > 2:
		h[0] = 0
	if h[0] == 2:
		if h[1] > 3:
			h[1] = 0
if m[0] > 5:
	m[0] = 0
print(''.join(map(str, h)) + ':' + ''.join(map(str, m)))


def solve():
	t = int(input())
	time = list(input())
	(h, m) = map(int, ''.join(time).split(':'))
	if t == 12:
		if h < 1:
			time[1] = '1'
		elif h > 12:
			if h % 10 == 0:
				time[0] = '1'
			else:
				time[0] = '0'
	elif h > 23:
		time[0] = '1'
	if m > 59:
		time[3] = '1'
	print(''.join(time))
solve()


def best(h, n, x):
	best = ''
	bestCnt = 10
	begin = 0
	end = n
	if x == 'h':
		if n == 12:
			begin = 1
			end = 13
	for i in range(begin, end):
		si = str(i)
		if len(si) == 1:
			si = '0' + si
		bad = 0
		for (x, y) in zip(h, si):
			if x != y:
				bad += 1
		if bad < bestCnt:
			bestCnt = bad
			best = si
	return best
n = int(input())
s = input()
h = s[0:2]
m = s[3:5]
print(best(h, n, 'h'), best(m, 60, 'm'), sep=':')


import sys
f = int(sys.stdin.readline())
s = str(sys.stdin.readline())
(hh, mm) = s.split(':')
h1 = int(hh[0])
h2 = int(hh[1])
m1 = int(mm[0])
m2 = int(mm[1])
if f == 12:
	if h1 > 1:
		if h2 < 3:
			h1 = 1
		else:
			h1 = 0
	if h1 == 0 and h2 == 0:
		h2 = 1
	elif h1 == 1:
		if h2 > 2:
			h2 = 1
elif h1 > 2 or (h1 == 2 and h2 > 3):
	h1 = 1
if m1 > 5:
	m1 = 1
print('%d%d:%d%d' % (h1, h2, m1, m2))