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

Difficulty: EASY

Solutions:
__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 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()


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)


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


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


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)


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


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)


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


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)


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)


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)


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)


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=':')


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


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


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


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


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)


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


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)


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:])))


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


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)


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]}')


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)


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


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)


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=':')