Problem:
 Score: 600 points    Problem Statement  Takahashi and Aoki are training for long-distance races in an infinitely long straight course running from west to east.  They start simultaneously at the same point and moves as follows towards the east :   Takahashi runs A_1 meters per minute for the first T_1 minutes,  then runs at A_2 meters per minute for the subsequent T_2 minutes, and alternates between these two modes forever.  Aoki runs B_1 meters per minute for the first T_1 minutes,  then runs at B_2 meters per minute for the subsequent T_2 minutes, and alternates between these two modes forever.   How many times will Takahashi and Aoki meet each other, that is, come to the same point? We do not count the start of the run. If they meet infinitely many times, report that fact.      Constraints   1 \leq T_i \leq 100000  1 \leq A_i \leq 10^{10}  1 \leq B_i \leq 10^{10}  A_1 \neq B_1  A_2 \neq B_2  All values in input are integers.         Input  Input is given from Standard Input in the following format:  T_1  T_2  A_1  A_2  B_1  B_2       Output  Print the number of times Takahashi and Aoki will meet each other. If they meet infinitely many times, print infinity instead.        Sample Input 1 1 2
10 10
12 4      Sample Output 1 1  They will meet just once, \frac{4}{3} minutes after they start, at \frac{40}{3} meters from where they start.       Sample Input 2 100 1
101 101
102 1      Sample Output 2 infinity  They will meet 101, 202, 303, 404, 505, 606, ... minutes after they start, that is, they will meet infinitely many times.       Sample Input 3 12000 15700
3390000000 3810000000
5550000000 2130000000      Sample Output 3 113  The values in input may not fit into a 32 -bit integer type.

Solution (Python):
import sys
stdin = sys.stdin
sys.setrecursionlimit(10**6)
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
nn = lambda: list(stdin.readline().split())
ns = lambda: stdin.readline().rstrip()

import math

t1,t2 = na()
a1,a2 = na()
b1,b2 = na()

p = a1*t1
q = b1*t1
x = p + a2*t2
y = q + b2*t2

if x < y:
  if p < q:
    print(0)
  elif p == q:
    print(1)
  elif p > q:
    print(int((p-q) / (y-x)) + math.ceil((p-q) / (y-x)))
elif x == y:
  print("infinity")
elif x > y:
  if p < q:
    print(int((q-p) / (x-y)) + math.ceil((q-p) / (x-y)))
  elif p == q:
    print(1)
  elif p > q:
    print(0)

Solution (Python):
T = list(map(int,input().split()))
A = list(map(int,input().split()))
B = list(map(int,input().split()))

d = [(A[i] - B[i])*T[i] for i in range(2)]
if d[0] < 0:
    d = [-d[0],-d[1]]
s = sum(d)
if s == 0:
    print('infinity')
elif s > 0:
    print(0)
else:
    print(d[0]//(-s)*2 + 1 - (d[0]%s == 0))

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

t1, t2, a1, a2, b1, b2 = map(int, read().split())
v1 = t1 * (a1 - b1)
v2 = t2 * (a2 - b2)
if v1 > 0:
    v1 *= -1
    v2 *= -1
if v1 + v2 == 0:
    print('infinity')
elif v1 + v2 < 0:
    print(0)
else:
    s, t = divmod(-v1, v1 + v2)
    ans = s * 2
    if t != 0:
        ans += 1
    print(ans)

Solution (Python):
t=list(map(int,input().split()))
a=list(map(int,input().split()))
b=list(map(int,input().split()))
if a[0]*t[0]+a[1]*t[1]==b[0]*t[0]+b[1]*t[1]:
    print('infinity')
elif a[0]*t[0]+a[1]*t[1]>b[0]*t[0]+b[1]*t[1]:
    if a[0]*t[0]>b[0]*t[0]:
        print(0)
    elif a[0]*t[0]==b[0]*t[0]:
        print(1)
    else:
        n=t[0]*(b[0]-a[0])
        m=t[0]*(a[0]-b[0])+t[1]*(a[1]-b[1])
        q=n//m
        if n%m==0:
            print(2*q)
        else:
            print(2*q+1)
else:
    if a[0]*t[0]<b[0]*t[0]:
        print(0)
    elif a[0]*t[0]==b[0]*t[0]:
        print(1)
    else:
        n=-t[0]*(b[0]-a[0])
        m=-t[0]*(a[0]-b[0])-t[1]*(a[1]-b[1])
        q=n//m
        if n%m==0:
            print(2*q)
        else:
            print(2*q+1)

Solution (Python):
def main():
    T1, T2 = map(int, input().split())
    A1, A2 = map(int, input().split())
    B1, B2 = map(int, input().split())
    dis1_a = A1 * T1
    dis1_b = B1 * T1
    dis2_a = A2 * T2
    dis2_b = B2 * T2

    if dis1_a + dis2_a == dis1_b + dis2_b:
        print('infinity')
        exit()

    if (dis1_a - dis1_b) > 0 and (dis2_a + dis1_a - dis2_b - dis1_b) > 0:
        print(0)
        exit()

    if (dis1_a - dis1_b) < 0 and (dis2_a + dis1_a - dis2_b - dis1_b) < 0:
        print(0)
        exit()

    
    
    
    

    diff = (dis1_a - dis1_b) - (dis2_a + dis1_a - dis2_b - dis1_b + (dis1_a - dis1_b))
    cnt = (dis1_a - dis1_b) // diff
    print(cnt * 2 if (dis1_a - dis1_b) % diff == 0 else cnt * 2 + 1)

    


if __name__ == "__main__":
    main()

Solution (Python):
INF = 10 ** 9
MOD = 10 ** 9 + 7
import sys
sys.setrecursionlimit(100000000)
dy = (-1,0,1,0)
dx = (0,1,0,-1)
from  math import factorial


def main():
    t1,t2 = map(int,input().split())
    a1,a2 = map(int,input().split())
    b1,b2 = map(int,input().split())
    if (a1 - b1) * t1 + (a2 - b2) * t2 == 0:
        ans = 'infinity'
    elif a1 == b1 or a2 == b2:
        ans = 'infinity'
    elif (a1 < b1 and a2 < b2) or (a1 > b1 and a2 > b2):
        ans = 0
    else:
        if a1 > b1 and a2 < b2:
            a1,b1 = b1,a1
            a2,b2 = b2,a2
        if (b1 - a1) * t1 + (b2 - a2) * t2 > 0:
            ans = 0
        else:
            x = (a1 - b1) * t1 + (a2 - b2) * t2
            if (b1 * t1 - a1 * t1) < x:
                ans = 1
            elif (b1 * t1 - a1 * t1)%x == 0:
                ans = 1 + 2 * ((b1 * t1 - a1 * t1)//x - 1) + 1
            else:
                ans = 1 + 2 * ((b1 * t1 - a1 * t1)//x)
    print(ans)
if __name__ == '__main__':
    main()

Solution (Python):
T_1, T_2 = map(int, input().split())
A_1, A_2 = map(int, input().split())
B_1, B_2 = map(int, input().split())

f = (A_1 - B_1) * T_1
s = (A_2 - B_2) * T_2

if f * (f + s) > 0:
	ans = 0
elif f * (f + s) == 0:
	ans = 'infinity'
else:
	ans = 1
	if f < 0:
		f *= -1
		s *= -1
	d = f + s
	div, mod = divmod(f, abs(d))
	ans += div * 2
	if mod == 0:
		ans -= 1

print(ans)

Solution (Python):


t1,t2=map(int,input().split())
a1,a2=map(int,input().split())
b1,b2=map(int,input().split())

d1=a1-b1
d2=a2-b2

if d1*d2>0:
    print(0)
else:
    if d1>0 and d2<0:
        plus=d1*t1+d2*t2
        if plus>0:
            print(0)
        elif plus==0:
            print("infinity")
        elif plus<0:
            d=-plus
            k=d1*t1
            if k%d==0:
                print(2*k//d)
            else:
                print(2*(k//d)+1)
    else:
        d1=-d1
        d2=-d2
        plus=d1*t1+d2*t2
        if plus>0:
            print(0)
        elif plus==0:
            print("infinity")
        elif plus<0:
            d=-plus
            k=d1*t1
            if k%d==0:
                print(2*k//d)
            else:
                print(2*(k//d)+1)

Solution (Python):
T1,T2=map(int,input().split())
A1,A2=map(int,input().split())
B1,B2=map(int,input().split())
OU=T1*(A1-B1)
HUKU=T2*(A2-B2)
TOTAL=OU+HUKU
if TOTAL==0:
 print("infinity")
elif (OU>0 and TOTAL>0) or (OU<0 and TOTAL<0):
 print(0)
elif (OU>0 and TOTAL*(-1)>OU) or (OU<0 and TOTAL*(-1)<OU):
 print(1)
else:
 K=int(OU/TOTAL)*-1
 if (OU%TOTAL)==0:
  print(K*2)
 else:
  print(K*2+1)

Solution (Python):
T1,T2,A1,A2,B1,B2=map(int,open(0).read().split())
D1,D2=(A1-B1)*T1,(A1-B1)*T1+(A2-B2)*T2
if D1*D2>0:print(0)
elif D2==0:print("infinity")
else:print(-D1//D2*2+1-(-D1%D2==0))

Solution (Python):
T1,T2,A1,A2,B1,B2 = map(int, open(0).read().split())
D1,D2 = (A1-B1)*T1, (A1-B1)*T1+(A2-B2)*T2
if D1*D2 > 0: print(0)
elif D2 == 0: print("infinity")
else:         print(-D1//D2*2+1 - (-D1%D2 == 0))

Solution (Python):
T1,T2,A1,A2,B1,B2=map(int, open(0).read().split())
D1,D2 = (A1-B1)*T1, (A1-B1)*T1+(A2-B2)*T2
if D1*D2 > 0:     print(0)
elif D2 == 0:     print("infinity")
elif -D1%D2 == 0: print(-D1//D2*2)
else:             print(-D1//D2*2+1)

Solution (Python):
T1,T2 = map(int,input().split())
A1,A2 = map(int,input().split()) 
B1,B2 = map(int,input().split()) 
D1,D2 = (A1-B1)*T1, (A1-B1)*T1+(A2-B2)*T2
if D1*D2 > 0:     print(0)
elif D2 == 0:     print("infinity")
elif -D1%D2 == 0: print(-D1//D2*2)
else:             print(-D1//D2*2+1)

Solution (Python):
T1,T2 = map(int,input().split())
A1,A2 = map(int,input().split()) 
B1,B2 = map(int,input().split()) 

D1,D2 = (A1-B1)*T1, (A1-B1)*T1+(A2-B2)*T2

if D1*D2 > 0:
    print(0)
elif D2 == 0:
    print("infinity")
else:
    if -D1%D2 == 0:
        print(-D1//D2*2)
    else:
        print(-D1//D2*2+1)

Solution (Python):
T1,T2 = map(int,input().split())
A1,A2 = map(int,input().split()) 
B1,B2 = map(int,input().split()) 


T = [T1]
f = 1
for i in range(3):
    if f:
        Tn = T2
        f = 0
    else:
        Tn = T1
        f = 1
    T.append(T[-1]+Tn)


class runner():
    def __init__(self,v1,v2):
        self.v1 = v1
        self.v2 = v2
        
    def distance(self,t):
        d = t//(T1+T2)*(self.v1*T1 + self.v2*T2)
        tm = t % (T1+T2)
        
        if tm < T1:
            d += self.v1 * tm
        else:
            d += self.v2*(tm-T1) + self.v1*T1
        return d

dif = runner(A1-B1,A2-B2)

c = -1
D = 0
DL = [0]
for t in T:
    pD = D
    D = dif.distance(t)
    DL.append(D)
    
    if pD*D <= 0:
        c += 1



if DL[1]*DL[2] > 0:
    print(0)
else:
    if DL[2] == 0:
        print("infinity")
    else:
        a = DL[2]
        if a>0:
            if -DL[1]%a == 0:
                print(-DL[1]//a*2)
            else:
                print(-DL[1]//a*2+1)
        else:
            if -DL[1]%a == 0:
                print(-DL[1]//a*2)
            else:
                print(-DL[1]//a*2+1)

Solution (Python):
t0, t1 = map(int, input().split())
a0, a1 = map(int, input().split())
b0, b1 = map(int, input().split())
d0 = (a0 - b0) * t0
d1 = (a1 - b1) * t1
if d0 < 0:
    d0, d1 = -d0, -d1
if d0 + d1 == 0:
    print('infinity')
    exit()
if d0 + d1 > 0:
    print(0)
    exit()
d = -(d0 + d1)
q, r = d0 // d, d0 % d
print(2 * q + min(r, 1))

Solution (Python):
t0, t1 = map(int, input().split())
a0, a1 = map(int, input().split())
b0, b1 = map(int, input().split())
d0 = (a0 - b0) * t0
d1 = (a1 - b1) * t1
if d0 < 0:
    d0, d1 = -d0, -d1
if d0 + d1 == 0:
    print('infinity')
    exit()
if d0 + d1 > 0:
    print(0)
    exit()
q, r = d0 // (-(d0+d1)), d0 % (-(d0+d1))
if r == 0:
    print(2 * q)
else:
    print(2 * q + 1)

Solution (Python):
import numpy as np
t1,t2 = map(int, input().split())
a1,a2 = map(int, input().split())
b1,b2 = map(int, input().split())

if a1 <= b1:
    a1,b1 = b1,a1
    a2,b2 = b2,a2


x1 = a1*t1 - b1*t1

x2 = x1 + a2*t2 - b2*t2
if x2 == 0:
    print("infinity")
elif x2 > 0:
    print(0)
else:
    ans =  int(np.ceil(x1/abs(x2))*2)
    if x1 % abs(x2) != 0:
        ans -= 1
    print(ans)

Solution (Python):

t1, t2 = map(int, input().split())
a1, a2 = map(int, input().split())
b1, b2 = map(int, input().split())
if (a1 > b1 and a2 > b2) or (a1 < b1 and a2 < b2):
    print(0)
else:
    if a1 < b1 and a2 > b2:
        a1, b1 = b1, a1
        a2, b2 = b2, a2
        
    if t1 * (a1 - b1) > t2 * (b2 - a2):
        print(0)
    elif t1 * (a1 - b1) == t2 * (b2 - a2):
        print('infinity')
    else:
        d = t2 * (b2 - a2) - t1 * (a1 - b1)
        y = t1 * (a1 - b1)
        
        if y % d == 0:
            print(y // d * 2)
        else:
            print(y // d * 2 + 1)

Solution (Python):
def solve(mid):
    pos = (d[0] + d[1]) * (mid // 2)
    if mid % 2 == 1:
        pos += d[0]

    if mid % 2 == 1:
        nxt_pos = pos + d[1]
    else:
        nxt_pos = pos + d[0]

    if pos * nxt_pos < 0 or nxt_pos == 0:
        return True
    else:
        return False

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

v0 = a[0] - b[0]
v1 = a[1] - b[1]
d = [t[0] * v0, t[1] * v1]

if d[0] == -d[1]:
    print("infinity")
    exit()
    
ok = 0
ng = 10 ** 60
while abs(ok - ng) > 1:
    mid = (ok + ng) // 2
    if solve(mid):
        ok = mid
    else:
        ng = mid
print(ok)

Solution (Python):
T1, T2, A1, A2, B1, B2 = map(int, open(0).read().split())
P = (A1-B1)*T1
Q = (A2-B2)*T2
if P>0:
  P *= -1
  Q *= -1
if P+Q<0:
  print(0)
elif P+Q==0:
  print('infinity')
else:
  S,T = divmod(-P,P+Q)
  if T!=0:
    print(S*2+1)
  else:
    print(S*2)

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    t = LI()
    a = LI()
    b = LI()
    a[0] -= b[0]
    a[1] -= b[1]
    if t[0]*a[0] == -t[1]*a[1]:
        print("infinity")
        return
    if a[0] < 0:
        a[0] *= -1
        a[1] *= -1
    if t[0]*a[0]+t[1]*a[1] > 0:
        print(0)
        return
    A = t[0]*a[0]+t[1]*a[1]
    k = t[0]*a[0]
    l = 0
    r = 10**20
    while r-l > 1:
        x = (l+r) >> 1
        if A*x+k > 0:
            l = x
        else:
            r = x
    ans = 2*l+1
    if A*r+k == 0:
        ans += 1
    print(ans)
    return


if __name__ == "__main__":
    solve()

Solution (Python):
t1,t2 = map(int, input().split())
a1,a2 = map(int, input().split())
b1,b2 = map(int, input().split())

d1=(a1-b1)*t1
d2=(a2-b2)*t2


if abs(d1) == abs(d2):
    print("infinity")
    exit()

if abs(d1) > abs(d2) or d1*d2>0:
    print(0)
    exit()

dif = abs(d2) - abs(d1)

ans=2*(abs(d1)//dif)
if abs(d1)%dif != 0:
    ans+=1
print(ans)

Solution (Python):
TAB = [list(map(int, input().split())) for _ in range(3)]
T = TAB[0]
A = TAB[1]
B = TAB[2]
dV1, dV2 = A[0] - B[0], A[1] - B[1]

dL1, dL2 = dV1 * T[0], dV2 * T[1]
if dL1 + dL2 == 0:
  print("infinity")
else:
  if dL1 * (dL1 + dL2) > 0:
    print(0)
  else:
    m = dL2 * T[0] - dL1 * T[1]
    n = dL1 + dL2
    m += n * T[1]
    cnt = m // (n * (T[0] + T[1]))
    if m % (n * (T[0] + T[1])) == 0:
      print(2*cnt-2)
    else:
      print(2*cnt-1)

Solution (Python):
"""from collections import *
from itertools import *
from bisect import *
from heapq import *

import math
from fractions import gcd"""

T1,T2=map(int,input().split())
A1,A2=map(int,input().split())
B1,B2=map(int,input().split())

A1*=T1
A2*=T2
B1*=T1
B2*=T2

if A1+A2==B1+B2:
    print("infinity")
else:
    if A1+A2<B1+B2:
        A1,B1=B1,A1
        A2,B2=B2,A2
    if A1>B1:
        print(0)
    else:
        print(((B1-A1)//(A1+A2-(B1+B2)))*2 +1-(((B1-A1)%(A1+A2-(B1+B2)))==0)*1)

Solution (Python):
t1,t2 = map(int,input().split())
a1,a2 = map(int,input().split())
b1,b2 = map(int,input().split())
p1 = t1*a1
q1 = t1*b1
p2 = p1+t2*a2
q2 = q1+t2*b2
if p2 == q2:
  print("infinity")
  exit()
if (p1>q1 and p2>q2) or (p1<q1 and p2<q2):
  print(0)
  exit()
x = abs(p1-q1)
y = abs(q2-p2)
if x%y == 0:
  ans = (x//y)*2
else:
  ans = (x//y)*2+1
print(ans)

Solution (Python):
T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())
v1, v2 = A1 - B1, A2 - B2
d1, d2 = v1 * T1, v2 * T2
if d1 > 0:
    d1 *= -1
    d2 *= -1

if d1 + d2 < 0:
    print(0)
elif d1 + d2 ==  0:
    print("infinity")
elif d1 + d2 > 0:
    s, t = divmod(-d1, d1+d2)
    print(2*s+(t!=0))

Solution (Python):
T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())
v1, v2 = A1 - B1, A2 - B2
d1, d2 = v1 * T1, v2 * T2
if d1 > 0:
    d1 *= -1
    d2 *= -1

if d1 + d2 < 0:
    print(0)
elif d1 + d2 ==  0:
    print("infinity")
elif d1 + d2 > 0:
    s, t = divmod(-d1, d1+d2)
    ans = 2*s + (t!=0)
    print(ans)

Solution (Python):
T1,T2,A1,A2,B1,B2=map(int, open(0).read().split())
C1,C2=A1-B1,A2-B2
if C1<0: C1,C2=-C1,-C2
Y1=C1*T1
Y2=Y1+C2*T2
if Y2>0:
    print(0)
elif Y2==0:
    print("infinity")
else:
    print(1+Y1//(-Y2)*2-(Y1%(-Y2)==0))

Solution (Python):
t1, t2 = map(int, input().split())
a1, a2 = map(int, input().split())
b1, b2 = map(int, input().split())
c1 = t1*(a1-b1)
c2 = t2*(a2-b2)
d = c1 + c2
if d == 0:
  print("infinity")
elif c1*c2 > 0 or c1*d > 0:
  print(0)
else:
  if abs(c1)%abs(d)==0:
    print((abs(c1)//abs(d))*2)
  else:
    print((abs(c1)//abs(d))*2+1)

Solution (Python):
t = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))

cycle = t[0] * (a[0] - b[0]) + t[1] * (a[1] - b[1])

if(cycle == 0):
    print('infinity')
    exit()

after_t1 = t[0] * (a[0] - b[0])

if(cycle * after_t1 > 0):
    print(0)
    exit()

after_t1 = abs(after_t1)
cycle = abs(cycle)

ans = 1 + 2 * (after_t1//cycle) - (after_t1 % cycle == 0) * 1

print(ans)

Solution (Python):
t1,t2=map(int,input().split())
a1,a2=map(int,input().split())
b1,b2=map(int,input().split())
if (a1>b1 and a2>b2) or (b1>a1 and b2>a2):
  print(0)
elif abs(a1-b1)*t1>abs(a2-b2)*t2:
  print(0)
elif abs(a1-b1)*t1==abs(a2-b2)*t2:
  print('infinity')
else:
  d1=abs(a1-b1)*t1
  d2=abs(a2-b2)*t2
  l=0
  r=10**18+1
  while r-l!=1:
    mid=(l+r)//2
    if d1*(mid+1)-d2*mid<0:
      r=mid
    else:
      l=mid
  if d1*(l+1)-d2*l==0:
    print(2*l)
  else:
    print(2*l+1)

Solution (Python):
T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())

P = (A1 - B1) * T1
Q = (A2 - B2) * T2
if P > 0:
	P *= -1
	Q *= -1

if P + Q < 0:
	print(0)
elif P + Q == 0:
	print("infinity")
else:
	S = (-P) // (P + Q)
	T = (-P) % (P + Q)
	if T != 0:
		print(S * 2 + 1)
	else:
		print(S * 2)

Solution (Python):
t1, t2 = map(int, input().split())
a1, a2 = map(int, input().split())
b1, b2 = map(int, input().split())

if t1 * a1 + t2 * a2 == t1 * b1 + t2 * b2:
    print("infinity")
    exit(0)
if t1 * a1 + t2 * a2 > t1 * b1 + t2 * b2:
    a1, b1 = b1, a1
    a2, b2 = b2, a2
if a1 < b1:
    print("0")
    exit(0)

l, r, labe, ans = 2, 10**18, False, 10**18

while l <= r:
    m = l + (r - l) // 2
    xa = (m - 1) * (t1 * a1 + t2 * a2) + t1 * a1
    xb = (m - 1) * (t1 * b1 + t2 * b2) + t1 * b1
    if xa == xb:
        labe = True
        ans = m
        break;
    elif xa < xb:
        ans = m
        r = m - 1
    else :
        l = m + 1

if labe:
    print(2 * (ans - 1))
else :
    print(2 * (ans - 1) - 1)

Solution (Python):
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))



T1,T2 = LI()
A1,A2 = LI()
B1,B2 = LI()

if A1 > B1 and A2 > B2:
    print(0)
    exit()
if A1 < B1 and A2 < B2:
    print(0)
    exit()

if A1 > B1:
    t = (A1-B1)*T1 / (B2-A2)
    delta = (A1-B1)*T1+(A2-B2)*T2
    if t == T2:
        print('infinity')
        exit()
    elif t > T2:
        print(0)
        exit()
    else:
        if delta > 0:
            print(math.floor((T2-t)*(B2-A2)/delta) + 1)
            exit()
        else:
            x = (A1-B1)*T1 / (-delta)
            if int(x) == x:
                print(2*int(x))
            else:
                print(2*math.floor(x)+1)

else:
    t = (B1-A1)*T1 / (A2-B2)
    delta = (B1-A1)*T1+(B2-A2)*T2
    if t == T2:
        print('infinity')
        exit()
    elif t > T2:
        print(0)
        exit()
    else:
        if delta > 0:
            print(math.floor((T2-t)*(A2-B2)/delta) + 1)
            exit()
        else:
            x = (B1-A1)*T1 / (-delta)
            if int(x) == x:
                print(2*int(x))
            else:
                print(2*math.floor(x)+1)

Solution (Python):
import sys
input = sys.stdin.buffer.readline

def main():
    T1,T2 = map(int,input().split())
    A1,A2 = map(int,input().split())
    B1,B2 = map(int,input().split())
    l = (A1-B1)*T1 + (A2-B2)*T2
    if l == 0:
        print("infinity")
    elif l > 0:
        if A1 > B1:
            print(0)
        else:
            d = (B1-A1)*T1
            if d%l == 0:
                ans = 2*(d//l)
            else:
                ans = 1+2*(d//l)
            print(ans)
    else:
        l *= -1
        if B1 > A1:
            print(0)
        else:
            d = (A1-B1)*T1
            if d%l == 0:
                ans = 2*(d//l)
            else:
                ans = 1+2*(d//l)
            print(ans)
    
if __name__ == "__main__":
    main()

Solution (Python):
T1, T2=map(int,input().split())
A1, A2=map(int,input().split())
B1, B2=map(int,input().split())

if A1 - B1 > 0:
  A1, B1 = B1, A1
  A2, B2 = B2, A2
  
d = (A1 - B1) * T1 + (A2 - B2) * T2

if d > 0:
  if ((B1 - A1) * T1) % d == 0:
    print(((B1 - A1) * T1 // d) *  2)
  else:
    print(((B1 - A1) * T1 // d) *  2 + 1)
elif d < 0:
  print(0)
else:
  print("infinity")

Solution (Python):
T1,T2 = map(int,input().split())
A1,A2 = map(int,input().split())
B1,B2 = map(int,input().split())

C1,C2 = B1-A1, B2-A2

if C1 > 0:
    if C1*T1+C2*T2 > 0:
        ans = 0
    elif C1*T1+C2*T2 == 0:
        ans = 'infinity'
    else:
        d = abs(C1*T1+C2*T2)
        if C1*T1%d==0:
            ans = 2*C1*T1//d
        else:
            ans = 2*(C1*T1//d)+1
else:
    if C1*T1+C2*T2 < 0:
        ans = 0
    elif C1*T1+C2*T2 == 0:
        ans = 'infinity'
    else:
        d = abs(C1*T1+C2*T2)
        dd = abs(C1*T1)
        if dd%d==0:
            ans = 2*dd//d
        else:
            ans = 2*(dd//d)+1
        
print(ans)

Solution (Python):
T1,T2 = map(int,input().split())
A1,A2 = map(int,input().split())
B1,B2 = map(int,input().split())
dif = T1*A1+T2*A2-T1*B1-T2*B2
if dif==0:
    print("infinity")
elif dif>0:
    if B1>A1:
        k = int(((B1-A1)*T1)/dif)
        if abs(((B1-A1)*T1)/dif-round(((B1-A1)*T1)/dif))<1e-20:
            print(2*round(((B1-A1)*T1)/dif))
        else:
            print(2*k+1)
    else:
        print(0)
else:
    if A1>B1:
        k = int(((A1-B1)*T1)/abs(dif))
        if abs((A1-B1)*T1/abs(dif)-round(((A1-B1)*T1)/abs(dif)))<1e-20:
            print(2*round(((A1-B1)*T1)/abs(dif)))
        else:
            print(2*k+1)
    else:
        print(0)

Solution (Python):
from _collections import deque
from _ast import Num


def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield (number)


input_parser = parser()


def gw():
    global input_parser
    return next(input_parser)


def gi():
    data = gw()
    return int(data)


MOD = int(1e9 + 7)

import numpy
from collections import deque
from math import sqrt
from math import floor


"""
DP1[i][j] =the maximum sum of deliciousness of dishes, which are chosen from 1st- to i-th dishes,
such that he can finish them in j minutes

still WA!!!

"""
t1 = gi()
t2 = gi()
a1 = gi()
a2 = gi()
b1 = gi()
b2 = gi()

if a1 < b1:
    a1, b1 = b1, a1
    a2, b2 = b2, a2



d1 = (a1 - b1) * t1
d2 = (a2 - b2) * t2

net = d1 + d2


def run():
    if d1 == 0 or net == 0:
        print('infinity')
        return
    elif net > 0:
        print(0)
        return

    d = abs(net)

    ans = 0
    p2 = (d1 // d)
    if (d1 % d == 0):
        ans = 2 * p2
    else:
        ans = 2 * p2 + 1

    print(ans)


run()

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

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
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 pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    t1,t2 = LI()
    a1,a2 = LI()
    b1,b2 = LI()

    if a1 > b1:
        a1,b1 = b1,a1
        a2,b2 = b2,a2

    sa = t1*a1 + t2*a2
    sb = t1*b1 + t2*b2

    if sa == sb:
        return "infinity"

    if sb > sa:
        return 0

    t = t1*b1 - t1*a1
    st = sa - sb
    if t % st == 0:
        r = t // st * 2
    else:
        r = t // st * 2 + 1

    return r


print(main())

Solution (Python):
import sys


T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())
runout = T1 * A1 - T1 * B1
TotalDiff = (T1 * A1 - T1 * B1) + (T2 * A2 - T2 * B2)

if TotalDiff == 0:
    print('infinity')
    sys.exit()
elif runout * TotalDiff > 0:
    print(0)
    sys.exit()

runout = abs(runout)
TotalDiff = abs(TotalDiff)

cnt = 0
cnt = (runout // TotalDiff) * 2
if runout % TotalDiff != 0:
    cnt += 1
print(cnt)

Solution (Python):
ma = lambda: map(int, input().split())
t,u = ma(); a,c = ma(); b,d = ma()
p = (a-b)*t; q = (c-d)*u
if p < 0: p *= -1; q *= -1
if p+q > 0: print(0)
elif p+q == 0: print('infinity')
else:
  t = -p-q
  print(1+(p//t)*2 if p%t else (p//t)*2)

Solution (Python):

import sys
INF = float("inf")


def solve(T: "List[int]", A: "List[int]", B: "List[int]"):

    if A[0] < B[0]:
        A, B = B, A

    if A[1] > B[1]:
        print(0)
        return
    elif A[0]*T[0]+A[1]*T[1] > B[0]*T[0]+B[1]*T[1]:
        print(0)
        return
    elif A[0]*T[0]+A[1]*T[1] == B[0]*T[0]+B[1]*T[1]:
        print("infinity")
        return

    K = (B[0]*T[0]+B[1]*T[1])-(A[0]*T[0]+A[1]*T[1])
    L = (A[0]-B[0])*T[0] // K
    if (B[0]-A[0])*T[0] % K == 0:
        print(2*L)
    else:
        print(2*L+1)
    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    T = [int(next(tokens)) for _ in range(2)]  
    A = [int(next(tokens)) for _ in range(2)]  
    B = [int(next(tokens)) for _ in range(2)]  
    solve(T, A, B)


if __name__ == '__main__':
    main()

Solution (Python):
t1,t2 = map(int,input().split())
a1,a2 = map(int,input().split())
b1,b2 = map(int,input().split())
if t1 * a1 + t2 * a2 == t1 * b1 + t2 * b2:
    print("infinity")
    exit()
if a1 < b1:
    tnp = a1
    a1 = b1
    b1 = tnp
    tnp = a2
    a2 = b2
    b2 = tnp
if (a1-b1) * t1 + (a2-b2) * t2 > 0:
    print(0)
    exit()
num = -(a1-b1) * t1 - (a2-b2) * t2
ans = 1

ans += ((a1-b1)*t1 // num)*2
if (a1-b1)*t1 % num == 0:
    ans -= 1
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
T = list(map(int, input().split()))
a = list(map(int, input().split()))
b = list(map(int, input().split()))

p = (a[0] - b[0]) * T[0]
q = (a[1] - b[1]) * T[1]

if p > 0:
  p *= -1
  q *= -1
if p + q < 0:
  print(0)
elif p + q > 0:
  print(-p // (p + q) * 2 + ((-p % (p + q)) > 0))
else:
  print("infinity")

Solution (Python):


import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

t1,t2 = LI()
a1,a2 = LI()
b1,b2 = LI()

x,y = a1*t1,b1*t1
z,w = a1*t1 + a2*t2, b1*t1 + b2*t2
if (x > y and z > w) or (y > x and w > z):
    print(0)
    quit()
if (z == w) :
    print('infinity')
    quit()
if x < y:
    x,y = y,x
if z < w:
    z,w = w,z
s1 = x - y
s2 = z - w
p = math.ceil(s1/s2)
q = s1 % s2
ans = 2*p - (q != 0)
print(ans)

Solution (Python):
T1, T2 = map(int, input().split())
A1, A2 = map(int, input().split())
B1, B2 = map(int, input().split())
a = A1*T1-B1*T1
b = A2*T2-B2*T2

if a<0:
    if b<0:
        print(0)
    elif b==0:
        print(0)
    elif b>0:
        if a<-b:
            print(0)
        elif a==-b:
            print('infinity')
        else:
            d = a+b
            
            if a%d==0:
                n = (-a)//d-1
                print(2*n+2)
            else:
                n = (-a)//d
                print(2*n+1)
elif a==0:
    if b<0:
        print(1)
    elif b==0:
        print('infinity')
    elif b>0:
        print(1)
elif a>0:
    if b<0:
        if a>-b:
            print(0)
        elif a==-b:
            print('infinity')
        else:
            d = -a-b
            
            if a%d==0:
                n = a//d-1
                print(2*n+2)
            else:
                n = a//d
                print(2*n+1)
    elif b==0:
        print(0)
    elif b>0:
        print(0)

Solution (Python):
import math
t1, t2, a1, a2, b1, b2 = [int(_) for _ in open(0).read().split()]
c1 = (b1 - a1) * t1
c2 = c1 + (b2 - a2) * t2
if c1 * c2 > 0:
    print(0)
    exit()
if c2 == 0:
    print('infinity')
    exit()
if c1 < 0:
    c1 *= -1
    c2 *= -1
ans = 2 * (c1 // (-c2)) + (c1 % c2 != 0)
print(ans)

Solution (Python):
t1, t2=map(int, input().split())
a1, a2=map(int, input().split())
b1, b2=map(int, input().split())

if (a1-b1)*(a2-b2)>0:
  print(0)
elif a1*t1+a2*t2==b1*t1+b2*t2:
  print('infinity')
else:
  if a1>b1:
    da=(a1-b1)*t1
    db=(b2-a2)*t2
    
    if da>db:
      print(0)
    else:
      temp=da//(db-da)
      if da%(db-da)>0:
        temp=2*temp
      else:
        temp=2*temp-1
      print(temp+1)
  else:
    db=(b1-a1)*t1
    da=(a2-b2)*t2
    
    if db>da:
      print(0)
    else:
      temp=db//(da-db)
      if db%(da-db)>0:
        temp=2*temp
      else:
        temp=2*temp-1
      print(temp+1)

Solution (Python):
T1,T2=map(int,input().split())
A1,A2=map(int,input().split())
B1,B2=map(int,input().split())
X=(B1-A1)*T1
Y=(B2-A2)*T2
if X>0:
    X=-X
    Y=-Y
if X+Y<0:
    print(0)
elif X+Y==0:
    print("infinity")
else:
    A=(-X)//(X+Y)
    B=(-X)%(X+Y)
    if B!=0:
        print(2*A+1)
    else:
        print(2*A)

Solution (Python):
def solve():
    from sys import stdin
    f_i = stdin
    
    T1, T2 = map(int, f_i.readline().split())
    A1, A2 = map(int, f_i.readline().split())
    B1, B2 = map(int, f_i.readline().split())
    
    P = (A1 - B1) * T1
    Q = (A2 - B2) * T2
    
    if P < 0:
        P *= -1
        Q *= -1
    
    cycle = P + Q
    if cycle > 0:
        return 0
    elif cycle == 0:
        return 'infinity'
    else:
        S, T = divmod(P, -cycle)
        if T == 0:
            return 2 * S
        else:
            return 2 * S + 1

print(solve())

Solution (Python):
import math
T1, T2 = map(int,input().split())
A1, A2 = map(int,input().split())
B1, B2 = map(int,input().split())

if A1*T1+A2*T2 == B1*T1+B2*T2:
    print('infinity')
elif (A1-B1)*T1/((A1-B1)*T1+(A2-B2)*T2) > 0:
    print(0)    
else:
    n = abs((A1-B1)*T1/((A1-B1)*T1+(A2-B2)*T2))
    meet = math.ceil(n) + int(n)
    print(meet)