Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	distance_table := [][]int{
		{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},
	}
	toll_table := [][]int{
		{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},
	}
	for scanner.Scan() {
		d, _ := strconv.Atoi(scanner.Text())
		if d == 0 {
			break
		}
		d--
		scanner.Scan()
		dbuf := strings.Split(scanner.Text(), " ")
		dh, _ := strconv.Atoi(dbuf[0])
		dm, _ := strconv.Atoi(dbuf[1])
		dtime := 100*dh+dm
		scanner.Scan()
		a, _ := strconv.Atoi(scanner.Text())
		a--
		scanner.Scan()
		abuf := strings.Split(scanner.Text(), " ")
		ah, _ := strconv.Atoi(abuf[0])
		am, _ := strconv.Atoi(abuf[1])
		atime := 100*ah+am
		toll := toll_table[d][a]
		if distance_table[d][a] <= 40 &&
			1730 <= dtime && dtime <= 1930 ||
			1730 <= atime && atime <= 1930 {
				toll = ((toll/2-1)/50+1)*50
		}
		fmt.Println(toll)
	}
}

Python: from collections import namedtuple

Data = namedtuple("Data", "rate distance")

SENTINEL = None
NOTHING = None
RATE_TABLE = [
    [SENTINEL],
    [SENTINEL, NOTHING, Data(300, 6), Data(500, 13), Data(600, 18), Data(700, 23), Data(1350, 43), Data(1650, 58)],
    [SENTINEL, Data(300, 6), NOTHING, Data(350, 7), Data(450, 12), Data(600, 17), Data(1150, 37), Data(1500, 52)],
    [SENTINEL, Data(500, 13), Data(350, 7), NOTHING, Data(250, 5), Data(400, 10), Data(1000, 30), Data(1350, 45)],
    [SENTINEL, Data(600, 18), Data(450, 12), Data(250, 5), NOTHING, Data(250, 5), Data(850, 25), Data(1300, 40)],
    [SENTINEL, Data(700, 23), Data(600, 17), Data(400, 10), Data(250, 5), NOTHING, Data(600, 20), Data(1150, 35)],
    [SENTINEL, Data(1350, 43), Data(1150, 37), Data(1000, 30), Data(850, 25), Data(600, 20), NOTHING, Data(500, 15)],
    [SENTINEL, Data(1650, 58), Data(1500, 52), Data(1350, 45), Data(1300, 40), Data(1150, 35), Data(500, 15), NOTHING]
]
DISTANCE_LIMIT = 40
TIME_LIMIT1 = 17 * 60 + 30
TIME_LIMIT2 = 19 * 60 + 30

while True:

    leave_number = int(input())

    if leave_number == 0:
        break

    leave_time = [int(item) for item in input().split(" ")]

    arrive_number = int(input())
    arrive_time = [int(item) for item in input().split(" ")]

    data = RATE_TABLE[leave_number][arrive_number]

    rate = data.rate

    if data.distance <= DISTANCE_LIMIT:

        time1 = leave_time[0] * 60 + leave_time[1]
        time2 = arrive_time[0] * 60 + arrive_time[1]

        if TIME_LIMIT1 <= time1 <= TIME_LIMIT2 or TIME_LIMIT1 <= time2 <= TIME_LIMIT2:
            rate //= 2

            if rate % 50 != 0:
                rate = rate + 50 - rate % 50

    print(rate)

Python: A = [
    [0, 300, 500, 600, 700, 1350, 1650],
    [6, 0, 350, 450, 600, 1150, 1500],
    [13, 7, 0, 250, 400, 1000, 1350],
    [18, 12, 5, 0, 250, 850, 1300],
    [23, 17, 10, 5, 0, 600, 1150],
    [43, 37, 30, 25, 20, 0, 500],
    [58, 52, 45, 40, 35, 15, 0],
]

while 1:
    d = int(input()); d -= 1
    if d == -1:
        break
    hd, md = map(int, input().split())
    dt = hd*100 + md
    a = int(input()); a -= 1
    ha, ma = map(int, input().split())
    at = ha*100 + ma
    if not a <= d:
        d, a = a, d
    if A[d][a] <= 40 and (1730 <= dt <= 1930 or 1730 <= at <= 1930):
        print((A[a][d] // 2 + 49)//50*50)
    else:
        print(A[a][d])