# AOJ 0163 Highway Toll
# Python3 2018.6.23 bal4u

g = [ \
	[ 0,  6, 13, 18, 23, 43, 58], \
	[ 6,  0,  7, 12, 17, 37, 52], \
	[13,  7,  0,  5, 10, 30, 45], \
	[18, 12,  5,  0,  5, 25, 40], \
	[23, 17, 10,  5,  0, 20, 35], \
	[43, 37, 30, 25, 20,  0, 15], \
	[58, 52, 45, 40, 35, 15,  0]]

f = [ \
	[   0,  300,  500,  600,  700, 1350, 1650], \
	[ 300,    0,  350,  450,  600, 1150, 1500], \
	[ 500,  350,    0,  250,  400, 1000, 1350], \
	[ 600,  450,  250,    0,  250,  850, 1300], \
	[ 700,  600,  400,  250,    0,  600, 1150], \
	[1350, 1150, 1000,  850,  600,    0,  500], \
	[1650, 1500, 1350, 1300, 1150,  500,    0]]

while 1:
	d = int(input())-1
	if d < 0: break
	h, m = map(int, input().split())
	ti = h*100 + m
	a = int(input())-1
	h, m = map(int, input().split())
	to = h*100 + m
	fee = f[d][a]
	if g[d][a] <= 40 and ((1730 <= ti and ti <= 1930) or (1730 <= to and to <= 1930)):
		fee = ((fee//2 - 1)//50 + 1)*50
	print(fee)
