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

namespace ConsoleApplication1
{
    class Program
    {
        static readonly DateTime inT = DateTime.Parse("17:30");
        static readonly DateTime outT = DateTime.Parse("19:30");

        static void Main(string[] args)
        {
            StringBuilder sb = new StringBuilder();

            int[][] table = new int[8][];
            table[1] = new int[] { 0, 0, 300, 500, 600, 700, 1350, 1650 };
            table[2] = new int[] { 0, 6, 0, 350, 450, 600, 1150, 1500 };
            table[3] = new int[] { 0, 13, 7, 0, 250, 400, 1000, 1350 };
            table[4] = new int[] { 0, 18, 12, 5, 0, 250, 850, 1300 };
            table[5] = new int[] { 0, 23, 17, 10, 5, 0, 600, 1150 };
            table[6] = new int[] { 0, 43, 37, 30, 25, 20, 0, 500 };
            table[7] = new int[] { 0, 58, 52, 45, 40, 35, 15, 0 };

            while (true)
            {
                int s = int.Parse(Console.ReadLine());

                if (s == 0) break;

                bool flg = false;

                string[] input = Console.ReadLine().Split(' ');
                int h = int.Parse(input[0]);
                int m = int.Parse(input[1]);
                flg = CheckTime(h, m);

                int t = int.Parse(Console.ReadLine());

                if (!flg)
                {
                    input = Console.ReadLine().Split(' ');
                    h = int.Parse(input[0]);
                    m = int.Parse(input[1]);
                    flg = CheckTime(h, m);
                }
                else Console.ReadLine();

                int min = Math.Min(s, t);
                int max = Math.Max(s, t);

                int p = table[min][max];

                if (flg && table[max][min] <= 40)
                {
                    p /= 2;

                    int q = p % 50;
                    if (q > 0) p = p - q + 50;
                }
                sb.AppendLine(p.ToString());
            }
            Console.Write(sb);
        }

        static bool CheckTime(int h, int m)
        {
            switch (h)
            {
                case 17:
                    return m >= 30;
                case 18:
                    return true;
                case 19:
                    return m <= 30;
                default:
                    return false;
            }
        }
    }
}