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