a,b,c=list(map(int, input().split()))

d=0

for _ in range(60):

    if c<=d+a:

        print(c)

        break

    d+=a+b

    while c<=d:c+=60

else: print((-1))
a,b,c=list(map(int, input().split()))

d,e=0,60

for _ in range(e):

    if c<=d+a:

        print(c)

        break

    d+=a+b

    c+=(d+e-c)//e*e

else: print((-1))
a, b, c = list(map(int, input().split()))
d = 0
for _ in range(60):
    if c <= d + a:
        print(c)
        break
    d += a + b
    while c <= d:
        c += 60
else:
    print((-1))

a, b, c = list(map(int, input().split()))
d, e = 0, 60
for _ in range(e):
    if c <= d + a:
        print(c)
        break
    d += a + b
    c += (d + e - c) // e * e
else:
    print((-1))

-d = 0 -for _ in range(60): +d, e = 0, 60 +for _ in range(e): -    while c <= d: -        c += 60 +    c += (d + e - c) // e * e