#include<iostream>
using namespace std;
int main(){
long p[8][8]={{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}};
long s,e,hs,ms,he,me,a,b,c;
while(cin>>s){
if(s==0)break;
cin>>hs>>ms>>e>>he>>me;
if(s>e){a=s;s=e;e=a;}
hs*=60;hs+=ms;
he*=60;he+=me;
a=(hs-1050)*(hs-1170);
b=(he-1050)*(he-1170);
if(a<=0||b<=0)c=1;else c=0;
if(p[e-1][s-1]<=40&&c==1)cout<<((p[s-1][e-1]/50+1)/2)*50<<endl;
else cout<<p[s-1][e-1]<<endl;
}
return 0;
}