using System;
using System.Collections.Generic;
using System.Linq;

namespace AOJ.Volume1
{
    public class HighwayToll
    {
        private struct Toll
        {
            public int From;
            public int To;
            public int Fee;
        }

        public static int Main()
        {
            DateTime st_limit = new DateTime(2000, 1, 1, 17, 30, 0);
            DateTime ed_limit = new DateTime(2000, 1, 1, 19, 30, 0);
            // 料金表
            var tolls = new List<Toll>()
            {
                new Toll{From = 1, To = 2, Fee = 300},
                new Toll{From = 1, To = 3, Fee = 500},
                new Toll{From = 1, To = 4, Fee = 600},
                new Toll{From = 1, To = 5, Fee = 700},
                new Toll{From = 1, To = 6, Fee = 1350},
                new Toll{From = 1, To = 7, Fee = 1650},
                new Toll{From = 2, To = 3, Fee = 350},
                new Toll{From = 2, To = 4, Fee = 450},
                new Toll{From = 2, To = 5, Fee = 600},
                new Toll{From = 2, To = 6, Fee = 1150},
                new Toll{From = 2, To = 7, Fee = 1500},
                new Toll{From = 3, To = 4, Fee = 250},
                new Toll{From = 3, To = 5, Fee = 400},
                new Toll{From = 3, To = 6, Fee = 1000},
                new Toll{From = 3, To = 7, Fee = 1350},
                new Toll{From = 4, To = 5, Fee = 250},
                new Toll{From = 4, To = 6, Fee = 850},
                new Toll{From = 4, To = 7, Fee = 1300},
                new Toll{From = 5, To = 6, Fee = 600},
                new Toll{From = 5, To = 7, Fee = 1150},
                new Toll{From = 6, To = 7, Fee = 500}
            };

            // 距離
            var dist = new int[8] { 0, 0, 6, 13, 18, 23, 43, 58 };

            while (true)
            {
                int start = int.Parse(Console.ReadLine());
                if (start == 0) { break; }

                var input = Console.ReadLine().Split(' ');
                int hour = int.Parse(input[0]);
                int minute = int.Parse(input[1]);
                DateTime st = new DateTime(2000, 1, 1, hour, minute, 0);

                int end = int.Parse(Console.ReadLine());
                input = Console.ReadLine().Split(' ');
                hour = int.Parse(input[0]);
                minute = int.Parse(input[1]);
                DateTime ed = new DateTime(2000, 1, 1, hour, minute, 0);

                // 時刻から割り引き対象か判定
                bool discount;
                if (st >= st_limit || ed <= ed_limit) { discount = true; }
                else { discount = false; }

                // 料金計算
                if (start > end)
                {
                    // 入れ替え
                    int tmp = start;
                    start = end;
                    end = tmp;
                }
                // 割引は距離が40km以下の場合
                if (dist[end] - dist[start] > 40) { discount = false; }

                var t = tolls.FirstOrDefault(x => x.From == start && x.To == end);
                if (discount)
                {
                    int ans = t.Fee / 2;
                    if (ans % 50 != 0)
                    {
                        if (ans % 100 > 50) { ans = ((ans / 100) + 1) * 100; }
                        else { ans = ans - (ans % 50) + 50; }
                    }
                    Console.WriteLine(ans);
                }
                else { Console.WriteLine(t.Fee); }
            }
            return 0;
        }
    }
}

Measured memory_bytes: 9596