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):
t1, t2 = map(int, input().split())
a1, a2 = map(int, input().split())
b1, b2 = map(int, input().split())

if ((a1 - b1) * (a2 - b2) > 0) or (abs(a1 - b1) * t1 > abs(a2 - b2) * t2):
  print(0)
elif abs(a1 - b1) * t1 == abs(a2 - b2) * t2:
  print("infinity")
else:
  d, m = divmod(abs(a1 - b1) * t1, abs(a2 - b2) * t2 - abs(a1 - b1) * t1)
  ans = d * 2
  if m > 0:
    ans += 1
  print(ans)

Solution (Python):
import sys
t1,t2=map(int,input().split())
a1,a2=map(int,input().split())
b1,b2=map(int,input().split())
n1=(b1-a1)*t1
n2=(b2-a2)*t2
if n1*n2>0 or abs(n1)>abs(n2):
  print(0)
  sys.exit()
if abs(n1)==abs(n2):
  print('infinity')
  sys.exit()
c=abs(n1)//abs(n1+n2)
while (c+1)*abs(n1+n2)<=abs(n1):
  c+=1
ans=2*c
if abs(n1)%(abs(n1+n2))!=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())


Ax = A1 * T1 + A2 * T2
Bx = B1 * T1 + B2 * T2
diff = abs(Ax - Bx)

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

if not ((A1 * T1 > B1 * T1 and Ax < Bx) or (A1 * T1 < B1 * T1 and Ax > Bx)):
    print(0)
    exit()


ans = 2 * ((abs(B1 - A1) * T1) // diff)
if (abs(B1 - A1) * T1) % diff != 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())
s1 = a1 - b1
s2 = a2 - b2


if s1<0:
    s1*=-1
    s2*=-1

d = s1*t1 + s2*t2
mxd = s1*t1

if d>0:
    print(0)
elif d==0:
    print('infinity')
else:
    if mxd%abs(d)==0:
        print(mxd//abs(d)*2)
    else:
        print(mxd//abs(d)*2+1)

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

C1 = A1 - B1
C2 = A2 - B2

if C1 * C2 >= 0:
    print(0)

else:
    if C1 < 0:
        C1, C2 = -C1, -C2
    P = T1 * C1
    Q = P + T2 * C2
    if Q > 0:
        print(0)
    elif Q == 0:
        print('infinity')
    else:
        hi = 10**18
        lo = -1
        while hi - lo > 1:
            mid = (hi + lo) // 2
            if Q * mid + P <= 0:
                hi = mid
            else:
                lo = mid
        if Q * hi + P == 0:
            print(hi * 2)
        else:
            print(hi * 2 - 1)

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



if A1>=B1:
    pass
else:
    A1,B1=B1,A1
    A2,B2=B2,A2

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

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

Solution (Python):

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


L1 = T1 * (A1- B1)
L2 = L1 + T2 * (A2- B2)

if L1 * L2 > 0:
    print(0)
elif L2 == 0:
    print("infinity")
else:
    M = (-L1) / L2
    ans = 2 * int(M) + 1
    if float(int(M)) == M:
        ans -= 1
    print(ans)

Solution (Python):






import sys
input = sys.stdin.readline

import math

import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache





MOD = 10**9+7
INF = float('inf')
AZ = "abcdefghijklmnopqrstuvwxyz"






def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]


def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()


def DD(arg): return defaultdict(arg)


def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n): return kaijo_memo[n]
  if(len(kaijo_memo) == 0): kaijo_memo.append(1)
  while(len(kaijo_memo) <= n): kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n): return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0): gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n): gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if n == r: return 1
  if n < r or r < 0: return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1: arr.append([temp, 1])
  if arr==[]: arr.append([n, 1])
  return arr


def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors


def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum


def gcd(a, b):
    while b: a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd (a, b)


def count_bit(n):
  count = 0
  while n:
    n &= n-1
    count += 1
  return count


def base_10_to_n(X, n):
  if X//n: return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

def base_10_to_n_without_0(X, n):
  X -= 1
  if X//n: return base_10_to_n_without_0(X//n, n)+[X%n]
  return [X%n]


def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count




T1,T2 = IL()
A1,A2 = IL()
B1,B2 = IL()
C1,C2 = B1-A1,B2-A2

if C1<0:
  C1,C2 = -C1,-C2

if T1*C1+T2*C2==0:
  print("infinity")
  exit()
elif T1*C1+T2*C2>0:
  print(0)
else:
  x = abs(T1*C1+T2*C2)
  print(T1*C1//x*2+1-int((T1*C1)%x==0))

Solution (Python):
'''
自宅用PCでの解答
'''
import math

import itertools
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit


ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    t1,t2 = map(int,ipt().split())
    a1,a2 = map(int,ipt().split())
    b1,b2 = map(int,ipt().split())
    d1 = t1*(a1-b1)
    d2 = t2*(a2-b2)
    if d1*(d1+d2) > 0:
        print(0)
    elif d1+d2 == 0:
        print("infinity")
    else:
        dt = d1+d2
        if abs(d1)%abs(dt):
            print(abs(d1)//abs(dt)*2+1)
        else:
            print(abs(d1)//abs(dt)*2)

    return None

if __name__ == '__main__':
    main()

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

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

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

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

d1 = (a1 - b1) * t1
d2 = (b2 - a2) * t2
k = d1 // (d2 - d1)
if d1 % (d2 - d1) == 0:
    print(2 * k)
else:
    print(2 * k + 1)

Solution (Python):
t1, t2 = map(int, input().split())
a1, a2 = map(int, input().split())
b1, b2 = map(int, input().split())
if a1>b1:
    a1, a2, b1, b2 = b1, b2, a1, a2
init = (a1-b1)*t1+(a2-b2)*t2
if init<0:
    print(0)
    exit(0)
elif init==0:
    print('infinity')
    exit(0)
lim = (b1-a1)*t1
if lim%init==0:
    print(lim//init*2)
else:
    print(lim//init*2+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 a1*t1 + a2*t2 == b1*t1 + b2*t2:
    print("infinity")
else:
    A = a1*t1 + a2*t2
    B = b1*t1 + b2*t2
    M1,m1 = max(a1*t1,b1*t1),min(a1*t1,b1*t1)
    M2,m2 = max(A,B),min(A,B)
    gap1,gap2 = M1-m1,M2-m2
    Mv,mv = max(a1,b1),min(a1,b1)
    vgap = Mv-mv
    possible = vgap*t1
    count = possible//gap2
    if possible%gap2 == 0:
        pitta = 1
    else:
        pitta = 0
    if (a1*t1 > b1*t1 and A > B) or (a1*t1 < b1*t1 and A < B):
        print(0)
    elif (a1*t1 > b1*t1 and A < B) or (a1*t1 < b1*t1 and A > B):
        if pitta == 1:
            print(1+count*2-1)
        else:
            print(1+count*2)

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 d1*d2>0:
    print(0)
    exit()

if d1<0:
    d1 *= -1
    d2 *= -1

if d1+d2>0:
    print(0)
    exit()

if d1+d2==0:
    print('infinity')
    exit()

t = -d2-d1

if d1%t==0:
    print(d1//t*2)
else:
    print(d1//t*2+1)

Solution (Python):

import sys
INF = -1
def main(T1, T2, A1, A2, B1, B2):
    D1 = T1 * (A1 - B1)
    D2 = D1 + T2 * (A2 - B2)

    if D1 * D2 > 0:
        return 0

    if abs(D1) == abs(D2) or D2 == 0:
        return INF

    d, m = divmod(abs(D1), abs(D2))
    return (1 if m else 0) + 2 * d


if __name__ == '__main__':
    input = sys.stdin.readline
    T1, T2 = map(int, input().split())
    A1, A2 = map(int, input().split())
    B1, B2 = map(int, input().split())
    ans = main(T1, T2, A1, A2, B1, B2)
    print(ans if ans >= 0 else 'infinity')

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:
  print(0)
elif A1<B1 and A2<B2:
  print(0)
elif T1*A1+T2*A2==T1*B1+T2*B2:
  print('infinity')
elif A1>B1 and A2<B2:
  if T1*A1+T2*A2>T1*B1+T2*B2:
    print(0)
  else:
    se=abs(T1*A1+T2*A2-(T1*B1+T2*B2))
    ad=T1*A1-T1*B1
    d=-(-ad//se)
    ans=2*d-1
    if ad%se==0:
      ans+=1
    print(ans)
elif A1<B1 and A2>B2:
  if T1*A1+T2*A2<T1*B1+T2*B2:
    print(0)
  else:
    se=abs(T1*A1+T2*A2-(T1*B1+T2*B2))
    ad=T1*B1-T1*A1
    d=-(-ad//se)
    ans=2*d-1
    if ad%se==0:
      ans+=1
    print(ans)

Solution (Python):


import sys
from typing import Any, Callable, Deque, Dict, List, Mapping, Optional, Sequence, Set, Tuple, TypeVar, Union






























def main():
    mod = 1000000007                
    inf = float('inf')              
    
    sys.setrecursionlimit(10**6)    
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def isp():   return input().split()
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())


    def solve():
        x = (a1 - b1) * t1
        y = (a2 - b2) * t2
        
        if x * (x + y) > 0:
            
            return 0
        elif abs(x) % abs(x + y) == 0:
            return 1 - 1 + 2 * (abs(x) // abs(x + y))
        else:
            return 1 + 2 * (abs(x) // abs(x + y))
        
    
    
    t1, t2 = mi()
    a1, a2 = mi()
    b1, b2 = mi()

    if a1 * t1 + a2 * t2 == b1 * t1 + b2 * t2:
        print('infinity')
    else:
        print(solve())


if __name__ == "__main__":
    main()

Solution (Python):
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

T1,T2 = map(int,readline().split())
A1,A2 = map(int,readline().split())
B1,B2 = map(int,readline().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())

d1 = T1*(A1-B1)
d2 = T2*(A2-B2)
if d1>d2:
    d1 *= -1
    d2 *= -1

if (d1>0 and d1+d2>0) or (d1<0 and d1+d2<0):
    print(0)
elif d1+d2==0:
    print("infinity")
else:
    v = (abs(d1)//(d1+d2))
    ans = v*2
    pos = d1+v*(d1+d2)
    if pos<0 and pos+d2>=0:
        ans += 1
    print(ans)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    T1, T2 = map(int, input().split())
    A1, A2 = map(int, input().split())
    B1, B2 = map(int, input().split())
    A = A1 * T1 + A2 * T2
    B = B1 * T1 + B2 * T2

    if A1 < B1 and A < B:
        print(0)
        return

    if A1 > B1 and A > B:
        print(0)
        return

    if A == B:
        print("infinity")
        return

    
    diff = abs(A - B)

    if T1 * abs(A1 - B1) % diff == 0:
        ans = T1 * abs(A1 - B1) // diff * 2
    else:
        ans = T1 * abs(A1 - B1) // diff * 2 + 1

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
from math import gcd


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)

MOD = 10 ** 9 + 7
INF = float("inf")


def main():
    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 = P * (-1)
        Q = Q * (-1)

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


if __name__ == "__main__":
    main()

Solution (Python):
import sys

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

if A1 * T1 + A2 * T2 > B1 * T1 + B2 * T2:
    print(0)
    sys.exit()

if A1 * T1 + A2 * T2 == B1 * T1 + B2 * T2:
    print('infinity')
    sys.exit()

N = (T1 * (A1 - B1) - 1) // (T1 * (B1 - A1) + T2 * (B2 - A2)) + 1

ans = N * 2 - 1

if N * (T1 * (B1 - A1) + T2 * (B2 - A2)) == T1 * (A1 - B1):
    ans += 1

print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

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

if T1*A1+T2*A2 == T1*B1+T2*B2:
    print("infinity")
    exit()
if (A1 < B1 and A2 < B2) or (B1 < A1 and B2 < A2):
    print(0)
    exit()

if A1 > B1:
    A1, B1 = B1, A1
    A2, B2 = B2, A2

if A1*T1+A2*T2 < B1*T1+B2*T2:
    print(0)
    exit()

F = A1*T1-B1*T1
L = A2*T2-B2*T2

S, T = divmod(-F, F+L)

if T == 0:
    print(2*S)
else:
    print(2*S+1)

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

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def main():
    T = NLI()
    A = NLI()
    B = NLI()
    gap = [a-b for a, b in zip(A, B)]

    if T[0]*gap[0] + T[1]*gap[1] == 0:
        print("infinity")
        exit()

    if gap[0]*gap[1] > 0 or abs(T[0]*gap[0]) > abs(T[1]*gap[1]):
        print(0)
        exit()

    x = abs(gap[0]*T[0])
    y = abs(T[0]*gap[0] + T[1]*gap[1])
    if x % y == 0:
        print(x // y * 2)
        exit()
    else:
        print(x // y * 2 + 1)



if __name__ == "__main__":
    main()

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:
  if((-P)%(P+Q)==0):
    print((-P)//(P+Q)*2)
  else:
    print((-P)//(P+Q)*2+1)

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


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:
    ans = "infinity"
else:
    if a1+a2 < b1+b2:
        a1,a2,b1,b2 = b1,b2,a1,a2
    
    if a1>b1:
        ans = 0
    else:
        ans = ((b1-a1) // (a1+a2-b1-b2)) * 2 + 1
        if (b1-a1) % (a1+a2-b1-b2) ==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())
x=(a1-b1)*t1
y=(a2-b2)*t2
if x>0:
  x*=-1
  y*=-1
if x+y<0:print(0)
elif x+y==0:print("infinity")
else:
  ans=(-x)//(x+y)
  if (-x)%(x+y):print(ans*2+1)
  else:print(ans*2)

Solution (Python):
t1,t2=map(int,input().split())
a1,a2=map(int,input().split())
b1,b2=map(int,input().split())
n1=(b1-a1)*t1
n2=(b2-a2)*t2
if n1*n2>0 or abs(n1)>abs(n2):
  print(0)
elif abs(n1)==abs(n2):
  print('infinity')
else:
  c=abs(n1)//abs(n1+n2)
  while (c+1)*abs(n1+n2)<=abs(n1):
    c+=1
  ans=2*c
  if abs(n1)%(abs(n1+n2))!=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())
first = a1 * t1 - b1 * t1
second = (a2 * t2 - b2 * t2)
last = second + first
if first * last > 0:
    ans = 0
elif (abs(last) == 0):
    ans = 'infinity'
else:
    s = abs(first) // abs(last)
    t = abs(first) % abs(last)
    ans = 2 * s
    if t != 0:
        ans += 1
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

t, tt = map(int, input().split())
a, aa = map(int, input().split())
b, bb = map(int, input().split())

if a*t+aa*tt == b*t+bb*tt or a*t == b*t:
    print('infinity')
    exit()

diff1 = a*t-b*t

diff12 = a*t+aa*tt - (b*t+bb*tt)

if diff1*diff12>0:
    print(0)
    exit()

diff1 = abs(diff1)
diff12 = abs(diff12)

if diff1%diff12==0:
    print(-(-diff1//diff12)*2)
else:
    print(-(-diff1//diff12)*2-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())
d = a1*t1+a2*t2-b1*t1-b2*t2
de = -a1*t1+b1*t1
if d == 0:
    print('infinity')
else:
    if a1<b1:
        if d < 0:
            print(0)
        else:
            if de%d == 0:
                print(2*(de//d))
            else:
                print(2*(de//d)+1)
    else:
        if d > 0:
            print(0)
        else:
            de = -de
            d = -d
            if de%d == 0:
                print(2*(de//d))
            else:
                print(2*(de//d)+1)

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

f_a = t[0]*a[0]
f_b = t[0]*b[0]
if f_a < f_b:
    x = 1
else:
    x = -1

s_a = f_a + t[1]*a[1]
s_b = f_b + t[1]*b[1]
if s_a < s_b:
    y = 1
else:
    y = -1

if s_a==s_b:
    print('infinity')
else:
    if x==y:
        print(0)
    else: 
        s = abs(f_a-f_b)
        t = abs(s_a-s_b)
        print((s//t*2)+(s%t!=0))

Solution (Python):
def main():
    t1, t2 = map(int, input().split())
    a1, a2 = map(int, input().split())
    b1, b2 = map(int, input().split())
    x, y = (a1-b1)*t1, (a2-b2)*t2
    if x//abs(x) == y//abs(y):
        print(0)
    else:
        if abs(x) == abs(y):
            print("infinity")
        else:
            x, y = abs(x), abs(y)
            if y < x:
                print(0)
            else:
                if x%(y-x) != 0:
                    print((x+(y-x)-1)//(y-x)*2-1)
                else:
                    print(x//(y-x)*2)

if __name__ == "__main__":
    main()

Solution (Python):
T = list(map(int, input().split()))
A = list(map(int, input().split()))
B = list(map(int, input().split()))
if T[0]* A[0] + T[1] * A[1] == T[0] * B[0] + T[1] * B[1]:
    print('infinity')
else:
    ans = 0
    A_pos = 0
    B_pos = 0
    before_state = -1 
    flag = True
    diff = abs(T[0]* A[0] + T[1]* A[1] - (T[0]* B[0] + T[1]* B[1]))
    diff_max = max(abs(A[0] - B[0])*T[0],  abs(A[1] - B[1])*T[1])
    max_num = int(diff_max/diff)
    up_num_first = 0
    for i in range(2):
        A_pos += T[i]* A[i]
        B_pos += T[i]* B[i]
        if A_pos > B_pos:
            if before_state == 0:
                up_num_first += 1
            before_state = 1
        elif A_pos < B_pos:
            if before_state == 1:
                up_num_first += 1
            before_state = 0
        elif A_pos == B_pos:
            up_num_first += 1
            before_state = -1
    A_pos_first = A_pos
    B_pos_first = B_pos
            
    up_num_second = 0
    for i in range(2):
        A_pos += T[i]* A[i]
        B_pos += T[i]* B[i]
        if A_pos > B_pos:
            if before_state == 0:
                up_num_second += 1
            before_state = 1
        elif A_pos < B_pos:
            if before_state == 1:
                up_num_second += 1
            before_state = 0
        elif A_pos == B_pos:
            up_num_second += 1
            before_state = -1

    A_pos = A_pos_first * (max_num - 1)
    B_pos = B_pos_first * (max_num - 1)
    up_num_final = 0
    for i in range(2):
        A_pos += T[i]* A[i]
        B_pos += T[i]* B[i]
        if A_pos > B_pos:
            if before_state == 0:
                up_num_final += 1
            before_state = 1
        elif A_pos < B_pos:
            if before_state == 1:
                up_num_final += 1
            before_state = 0
        elif A_pos == B_pos:
            up_num_final += 1
            before_state = -1
    if max_num > 1:
        ans = up_num_first  + (max_num - 2) * up_num_second  + up_num_final
    else:
        ans = up_num_first  + (max_num - 1) * up_num_second
    print(ans)

Solution (Python):
t1,t2=map(int,input().split())
a,b=map(int,input().split())
c,d=map(int,input().split())
x=t1*(a-c)
y=t2*(b-d)
if x<0:
  x=-x
  y=-y
if x+y==0:
  print('infinity')
elif x+y>0:
  print(0)
else:
  print(min(1,x%abs(x+y))+2*(x//abs(x+y)))

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

x1 = (a1-b1) * t1
x2 = (a2-b2) * t2 + x1
      
if x2 ==0:
    print("infinity")
elif x1*x2>0:
    print(0)
else:
    print(abs(x1) // abs(x2) * 2 + (abs(x1)%abs(x2) > 0))

Solution (Python):
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]

    T = LI()
    A = LI()
    B = LI()

    p1 = (A[0] - B[0]) * T[0]
    p2 = p1 + (A[1] - B[1]) * T[1]
    if p1 * p2 > 0:
        print(0)
        return
    elif p2 == 0:
        print('infinity')
        return

    ans = 1
    p2 = abs(p2)
    p1 = abs(p1)
    if p1 % p2 == 0:
        ans -= 1
    ans += (p1 // p2) * 2
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    T = LI()
    A = LI()
    B = LI()

    p1 = (A[0] - B[0]) * T[0]
    p2 = p1 + (A[1] - B[1]) * T[1]
    if p1 * p2 > 0:
        print(0)
        return
    elif p2 == 0:
        print('infinity')
        return

    ans = 1
    if p2 + p1 == 0:
        print(2)
        return
    p2 = abs(p2)
    p1 = abs(p1)
    if p2 > p1:
        print(1)
        return
    if p1 % p2 == 0:
        ans -= 1
    ans += (p1 // p2) * 2
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
from math import ceil
t1, t2 = map(int, input().split())
a1, a2 = map(int, input().split())
b1, b2 = map(int, input().split())
a = t1 * (a1 - b1)
b = t2 * (a2 - b2)
if a > 0:
    a *= -1
    b *= -1
if a + b < 0:
    print(0)
elif a + b > 0:
    if (-1 * a) % (a + b):
        print(((-1 * a) // (a + b)) * 2 + 1)
    else:
        print((-1 * a) // (a + b) * 2)
else:
    print("infinity")

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

a, b = A1 * T1, A2 * T2
c, d = B1 * T1, B2 * T2

if a + b == c+ d:
    print("infinity")
    exit()
if a + b < c+d:
    a,b,c,d = c,d,a,b
if a > c:
    print(0)
    exit()
else:
    num = 0
    if (c-a) % (a+b-c-d)  == 0:
        num = 1
    print((c-a-1) // (a+b-c-d) * 2+num+1)

Solution (Python):
a,b,c,d,e,f=map(int,open(0).read().split());g,h=(c-e)*a,(d-f)*b
if g>0:g,h=-g,-h
i=g+h
if i<0:print(0)
elif i==0:print("infinity")
else:print(2*(-g//i)+[0,1][int(bool(-g%i))])

Solution (Python):
t1,t2=[int(j) for j in input().split()]
a1,a2=[int(j) for j in input().split()]
b1,b2=[int(j) for j in input().split()]
c1,c2=a1-b1,a2-b2
d1,d2=c1*t1,c2*t2
if d1>0:
    d1=-d1
    d2=-d2
if d1+d2<0:
    print(0)
elif d1+d2==0:
    print("infinity")
else:
    print(2*(-d1//(d2+d1))+[0,1][int(bool(-d1%(d2+d1)))])

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

if A1 < B1:
  A1, A2, B1, B2 = B1, B2, A1, A2

xa = A1*T1 + A2*T2
xb = B1*T1 + B2*T2
if xa > xb:
  print(0)
elif xa == xb:
  print('infinity')
else:
  d1 = (A1 - B1) * T1
  d2 = xb - xa
  n = d1 // d2
  if d1 % d2 == 0:
    print(2*n)
  else:
    print(2*n + 1)

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

S = [0]*2
for i in range(2):
    S[0] += T[i]*A[i]
    S[1] += T[i]*B[i]
if S[0]==S[1]:
    print("infinity")
else:
    if S[0]<S[1]:
        A,B = B,A
        S = S[::-1]
    if A[0]==B[0] or A[1]==B[1]:
        print(0)
    elif A[0]>B[0] and A[1]>B[1]:
        print(0)
    elif A[0]>B[0] and A[1]<B[1]:
        print(0)
    elif A[0]<B[0] and A[1]>B[1]:
        S = S[0]-S[1]
        T = T[0]*(B[0]-A[0])
        print(T//S*2-1+(1 if T%S==0 else 2))

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

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    T=LI()
    A=LI()
    B=LI()
    
    SA=T[1]*A[1]+T[0]*A[0]
    SB=T[1]*B[1]+T[0]*B[0]
    
    if SA == SB:
        print("infinity")
    else:
        
        if SB>SA:
            SB,SA=SA,SB
            A,B=B,A
        
        if A[0]>=B[0]:
            print(0)
        else:
            diff1=(B[0]-A[0])*T[0]
            diff2=SA-SB
            cnt=(diff1//diff2+1)
            ans=(cnt)*2-1
            
            
            if diff2*(cnt-1) == diff1:
                ans-=1
            print(ans)

main()

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    T=LI()
    A=LI()
    B=LI()
    
    
    d1=(A[0]-B[0])*T[0]
    d2=(A[1]-B[1])*T[1]
    
    if d1>=0:
        
        d1*=-1
        d2*=-1
        
    
    if d1+d2==0:
        print("infinity")
    elif d1==0:
        print(0)
    elif d1+d2<=0:
        print(0)
    else:
        cnt=(-1*d1)//(d1+d2)
        ans=cnt*2+1
        if cnt*(d1+d2)==-1*d1:
            ans-=1
        print(ans)
        
        
        
    

main()

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