def solve(a, b, c):

    l = 0; r = a;

    t = 0

    for i in range(80):

        t = l // 60

        p = 60*t + c

        if l <= p <= r:

            print(p)

            exit()



        l = r + b

        r = l + a



    print((-1))



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

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

l = 0; r = a;



for i in range(80):

    t = l // 60

    p = 60*t + c

    if l <= p <= r:

        print(p)

        exit()



    l = r + b

    r = l + a



print((-1))
def solve(a, b, c):
    l = 0
    r = a
    t = 0
    for i in range(80):
        t = l // 60
        p = 60 * t + c
        if l <= p <= r:
            print(p)
            exit()
        l = r + b
        r = l + a
    print((-1))


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

a, b, c = list(map(int, input().split()))
l = 0
r = a
for i in range(80):
    t = l // 60
    p = 60 * t + c
    if l <= p <= r:
        print(p)
        exit()
    l = r + b
    r = l + a
print((-1))

-def solve(a, b, c): -    l = 0 -    r = a -    t = 0 -    for i in range(80): -        t = l // 60 -        p = 60 * t + c -        if l <= p <= r: -            print(p) -            exit() -        l = r + b -        r = l + a -    print((-1)) - - -solve(a, b, c) +l = 0 +r = a +for i in range(80): +    t = l // 60 +    p = 60 * t + c +    if l <= p <= r: +        print(p) +        exit() +    l = r + b +    r = l + a +print((-1))