a,b=map(int,input().split()) c=3928 for i in range(12): t=3927*i+3*i//11-a*3600-b*60 if t>=0:c=min(c,t) print(c)