A,B=map(int,input().split()) C=A*3600+B*60 P=10**20 for i in range(11): P=min(P,int((3600/5.5)*60*i-C)%43200) print(P)