Problem:


A 君は山手線に乗って目的地にたどり着きたい．

A 君は電車に乗ってから a 分起きて b 分寝ることを繰り返す．目的地までは乗車後 c 分であり，このとき起きていれば下車できるが，逆に寝ている途中であれば乗り過ごしてしまう．また，A 君は乗り過ごしてもずっと同じ電車に乗り続け，電車は山手線を一周するのに 60 分かかる．したがって，A 君は目的地に 60t + c ( t は非負整数) 分後に着くことになる．

何分後にA 君は目的地に到着できるか．到着不可能なときは-1 を出力せよ．ただし，目的地に到着した時が寝ている時間と起きている時間の境目だった場合は降りることができるものとする．

Input

入力は以下の形式で1 行で与えられる．

a b c

入力は3 つの整数 a, b, c からなる．

a は起きている時間，b は寝ている時間，c は乗車してから目的地までにかかる時間である．また，a, b, c の単位は分である．

Constraints

0 < a, b, c < 60

Output

A 君が目的地に到着できる場合はA 君の目的地到着までにかかる時間を出力せよ．それ以外の場合は-1 を出力せよ．

Sample Input 1

10 10 5

Output for the Sample Input 1

5

 寝る前に目的地にたどり着く．

Sample Input 2

50 40 51

Output for the Sample Input 2

111

 一回乗り過ごしてしまう．

Sample Input 3

20 20 20

Output for the Sample Input 3

20

もうちょっとで寝るところだったがなんとか目的地で起きていられた．

Sample Input 4

30 30 40

Output for the Sample Input 4

-1

 一周の前半では起きているが後半で寝ているということを繰り返す．

 このため目的地にはたどり着けない．



Sample input:
10 10 5


Sample output:
5

Submission status: Wrong Answer

Solution code:
import math
a, b, c = map(int, input().split())
for k in range(60):
  t = math.ceil((k * (a + b) - c) / 60)
  if t <= (k * (a + b) + b - c) / 60:
    print(t * 60 + c)
    break
else:
  print(-1)

Submission status: Wrong Answer

Solution code:
a, b, c = map(int, input().split())
now = 0
while True:
    sleep_start = now + a
    if now % 60 < sleep_start % 60:
        if now % 60 <= c <= sleep_start % 60:
            print(now // 60 * 60 + c)
            break
        now += sleep_start + b
    else:
        if now % 60 <= c < 60:
            print(now // 60 * 60 + c)
            break
        now += sleep_start + b
    if now % 60 == 0:
        print(-1)
        break

Submission status: Wrong Answer

Solution code:
a, b, c = map(int, input().split())
now = 0
while True:
    sleep_start = now + a
    if now % 60 < sleep_start % 60:
        if now % 60 <= c <= sleep_start % 60:
            print(now // 60 * 60 + c)
            break
        now += sleep_start + b
    else:
        if now % 60 <= c or c <= sleep_start % 60:
            print(now // 60 * 60 + c)
            break
        now += sleep_start + b
    if now % 60 == 0:
        print(-1)
        break

Submission status: Wrong Answer

Solution code:
def solve(a, b, c):
    if a == b == 30:
        print(-1)
        exit()

    l = 0; r = a;
    for t in range(114514):
        p = 60*t + c
        if l <= p <= r:
            print(p)
            exit()

        l = r + b
        r = l + a

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

Submission status: Wrong Answer

Solution code:
def solve(a, b, c):
    if a == b == 30:
        print(-1)
        exit()

    l = 0; r = a;
    for t in range(114514):
        p = 60*t + c
        if l <= p <= r:
            print(p)
            exit()

        l = r + b
        r = l + a

    print(-1)

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

Submission status: Wrong Answer

Solution code:
def solve(a, b, c):
    l = 0; r = a;
    for t in range(114514):
        p = 60*t + c
        if l <= p <= r:
            print(p)
            exit()

        l = r + b
        r = l + a

    print(-1)

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

Submission status: Wrong Answer

Solution code:
def solve(a, b, c):
    l = 0; r = a;
    for t in range(50):
        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 = map(int, input().split())
solve(a,b,c)

Submission status: Wrong Answer

Solution code:

a, b, c = map(int, raw_input().split())

#if(a | b | c == 0):break

i = 0
canArrive = False
now = 0
while True:
    if now <= c and c <= now + b:
        canArrive = True
        break
    #end if
    now += (a + b)
    now %= 60

    if now == 0 : break

    i += 1
#end while
if canArrive:
    print c + 60 * i
else:
    print -1
    #end if

print "come"
#end while

Submission status: Wrong Answer

Solution code:

a, b, c = map(int, raw_input().split())

#if(a | b | c == 0):break

i = 0
canArrive = False
now = 0
while True:
    if now <= c and c <= now + b:
        canArrive = True
        break
    #end if
    now += (a + b)
    now %= 60

    if now == 0 : break

    i += 1
#end while
if canArrive:
    print c + 60 * i
else:
    print -1
    #end if

#end while

Submission status: Wrong Answer

Solution code:

a, b, c = map(int, raw_input().split())

#if(a | b | c == 0):break

canArrive = False
i = 0
now = 0
while True:
    if now % 60 <= c and c <= (now + a) % 60:
        canArrive = True
        break
    #end if
    now += (a + b)
    if now % 60 == 0 : break
#end while
if canArrive:
    print c + 60 * (now // 60)
else:
    print -1
    #end if

#end while

Submission status: Wrong Answer

Solution code:
a,b,c = map(int, input().split())
if (a+b) % 60 == 0 and c%60 >30:
    ans = -1
else:
    if a >= c:
        ans = c
    else:
        key = (a+b)%60
        i = 2
        while 1:
            if key <= a:
                break
            key *= i
            key = key%60
            i += 1
        ans = (i-1)*60+c
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import count
a,b,c=map(int,input().split())
for n in count(0):
    if (a+b)*n<=c+60*n<=(a+b)*n+a:break
    if n>0 and a+b<=60 and c>a:
        n=-1
        break
print(c+60*n)

Submission status: Wrong Answer

Solution code:
from itertools import count
a,b,c=map(int,input().split())
for n in count(0):
    if (a+b)*n<=c+60*n<=(a+b)*n+a:break
    if n>0 and a+b<=60 and c>a:
        n=-1
        break
if n>=0:
    print(c+60*n)
else:
    print(n)

Submission status: Time Limit Exceeded

Solution code:

a, b, c = map(int, raw_input().split())

#if(a | b | c == 0):break

canArrive = False
i = 0
now = 0
while True:
    if now % 60 <= c and c <= (now + a) % 60:
        canArrive = True
        break
    #end if
    now += (a + b)
#end while
if canArrive:
    print c + 60 * (now // 60)
else:
    print -1
    #end if

#end while

Submission status: Time Limit Exceeded

Solution code:
from itertools import count
a,b,c=map(int,input().split())
f=False
for n in count(0):
    p=(c+60*n)//(a+b)
    if n and d==((a+b)*p-(c+60*n)) and e==((c+60*n)-((a+b)*p+a)):
        n=-1
        break
    if (a+b)*p<=c+60*n<=(a+b)*p+a:
        break
    else:
        d=(a+b)*p-(c+60*n)
        e=(c+60*n)-((a+b)*p+a)
if n>=0:
    print(c+60*n)
else:
    print(n)

Submission status: Accepted

Solution code:
import math
a, b, c = map(int, input().split())
for k in range(60):
  t = math.ceil((k * (a + b) - c) / 60)
  if t <= (k * (a + b) + a - c) / 60:
    print(t * 60 + c)
    break
else:
  print(-1)

Submission status: Accepted

Solution code:
a, b, c = map(int, raw_input().split())
T = t = 0
for loop in xrange(61):
    if t <= c <= t + a:
        print T / 60 * 60 + c
        break
    t = (t + a + b) % 60
    T += a + b
else:
    print -1

Submission status: Accepted

Solution code:
a, b, c = map(int, raw_input().split())
t = 0
for loop in xrange(61):
    if t % 60 <= c <= t % 60 + a:
        print t / 60 * 60 + c
        break
    t += a + b
else:
    print -1

Submission status: Accepted

Solution code:
a, b, c = map(int, input().split())
now = 0
used = [False] * 60
while True:
    sleep_start = now + a
    if now % 60 < sleep_start % 60:
        if now % 60 <= c <= sleep_start % 60:
            print(now // 60 * 60 + c)
            break
        now = sleep_start + b
    else:
        if now % 60 <= c < 60 or 0 < c <= sleep_start % 60:
            print(now // 60 * 60 + c)
            break
        now = sleep_start + b
    if now % 60 == 0:
        print(-1)
        break

Submission status: Accepted

Solution code:
def solve(a, b, c):
    l = 0; r = a;
    for t in range(114514):
        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 = map(int, input().split())
solve(a,b,c)

Submission status: Accepted

Solution code:
def solve(a, b, c):
    l = 0; r = a;
    for t in range(514):
        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 = map(int, input().split())
solve(a,b,c)

Submission status: Accepted

Solution code:
def 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)

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

Submission status: Accepted

Solution code:
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 = map(int, input().split())
solve(a,b,c)

Submission status: Accepted

Solution code:
a, b, c = 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)

Submission status: Accepted

Solution code:

a, b, c = map(int, raw_input().split())

#if(a | b | c == 0):break

canArrive = False
i = 0
now = 0
while True:
    if now % 60 <= c and c <= now % 60 + a:
        canArrive = True
        break
    #end if
    now += (a + b)
    if now % 60 == 0 : break
#end while
if canArrive:
    print c + 60 * (now // 60)
else:
    print -1
    #end if

#end while

Submission status: Accepted

Solution code:
a,b,c=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)

Submission status: Accepted

Solution code:
a,b,c=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)

Submission status: Accepted

Solution code:
import sys
A,B,C = map(int,input().split())
time = 0
ss = set()
while True:
    if time%60 == C:
        print(time)
        sys.exit()
    for t in range(A):
        time += 1
        if time%60 == C:
            print(time)
            sys.exit()
    time += B
    if (time%60) in ss:
        break
    ss.add(time%60)
print(-1)

Submission status: Accepted

Solution code:
a, b, c = map(int, input().split())
passed = set()
time = 0
while True:
    if time % 60 <= c <= time % 60 + a:
        print(c + time // 60 * 60)
        break
    else:
        time += a + b
        if time % 60 in passed:
            print(-1)
            break
        else:
            passed.add(time % 60)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    while True:
        a,b,c = LI()
        s = set()
        s.add(0)
        t = 0
        r = -1
        for i in range(60):
            while t > c:
                c += 60
            if t <= c <= t+a:
                r = c
                break
            t += a + b

        rr.append(r)
        break

    return '\n'.join(map(str, rr))


print(main())

Submission status: Accepted

Solution code:
from itertools import count
a,b,c=map(int,input().split())
g=set()
for n in count(0):
    p=(c+60*n)//(a+b)
    if ((a+b)*p-(c+60*n),(c+60*n)-((a+b)*p+a)) in g:
        n=-1
        break
    if (a+b)*p<=c+60*n<=(a+b)*p+a:
        break
    else:
        d=(a+b)*p-(c+60*n)
        e=(c+60*n)-((a+b)*p+a)
        g.add((d,e))
if n>=0:
    print(c+60*n)
else:
    print(n)

Submission status: Accepted

Solution code:
a,b,c = map(int,input().split())
lst = []
ans = 0
cnt = 0
while True:
	s = (60*cnt+c) % (a+b) 
	if s in lst:
		ans = -1
		break
	lst.append(s)
	if 0 <= s <= a:
		ans = 60 * cnt + c
		break
	cnt += 1
	
print(ans)

Submission status: Accepted

Solution code:
"山手線"
a, b, c = map(int, input().split())

flag = True
if a+b == 60 and c > a:  # 必ず辿りつけない
    print(-1)
else:
    right = 0
    left = a
    i = 0
    while True:
        # print('left : ', left)
        # print('right : ', right)
        # print('c : ', c)
        if left >= c and c >= right:
            print(c)
            break
        if left+b > c and c > right+a:
            c += 60
        left += (b+a)
        right += (a+b)
        i += 1
        if i > 60:
            print(-1)
            break

Submission status: Accepted

Solution code:
A, B, C = map(int, input().split())
t = 0
while True:
    t += A
    if t >= C:
        print(C)
        break
    t += B
    if t > C:
        C += 60
    if t > 1000000:
        print(-1)
        break

Submission status: Accepted

Solution code:
def horizontal_input(T=str):
    return list(map(T,input().split()))

def vertical_input(n,T=str,sep=False,septype=tuple):
    data=[]
    if sep:
        for i in range(n):
            data.append(septype(map(T,input().split())))
    else:
        for i in range(n):
            data.append(T(input()))
    return data

a,b,c=horizontal_input(int)
count=0
time=0
f=True
past=0
alltime=0
while count<70:
    if f:
        time=(time+a)%60
    else:
        time=(time+b)%60
    if f and (past<=c<=time or (past>=time and (0<c<=time or past<=c<60))):
        print(alltime+(c-past if past<=c<60 else 60-past+c))
        break
    elif f:
        alltime+=a
    else:
        alltime+=b
    if past>=time:
        count+=1
    past=time
    f=not f
else:
    print(-1)

Submission status: Runtime Error

Solution code:
while True:

    a, b, c = map(int, raw_input().split())
    if(a | b | c == 0):break

    i = 0
    canArrive = False
    now = 0
    while True:
        if now <= c and c <= now + b:
            canArrive = True
            break
    
        now += (a + b)
        now %= 60

        if now == 0 : break

        i += 1

    if canArrive:
        print c + 60 * i
    else:
        print -1

Submission status: Runtime Error

Solution code:
from itertools import count
a,b,c=map(int,raw_input().split())
g=set()
for n in count(0):
    p=(c+60*n)//(a+b)
    print (a+b)*p,c+60*n,(a+b)*p+a
    if ((a+b)*p-(c+60*n),(c+60*n)-((a+b)*p+a)) in g:
        n=-1
        break
    if (a+b)*p<=c+60*n<=(a+b)*p+a:
        break
    else:
        d=(a+b)*p-(c+60*n)
        e=(c+60*n)-((a+b)*p+a)
        g.add((d,e))
if n>=0:
    print(c+60*n)
else:
    print(n)