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 in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <iostream>
#include <string>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>
#include <array>
#include <queue>
#include <map>
#include <stack>
typedef long long int llint;

template<long long int MODVALUE> class modllint{
  typedef long long int llint;
  llint val;
  void mod(){
    while( this->val < 0 ) this->val += MODVALUE;
    this->val %= MODVALUE;
  }
  void assign(const llint val){
    this->val = val;
    mod();
  }
public:
  modllint(): val(0){};
  modllint(const llint val){assign(val);};
  
  modllint& operator+=(const llint val){
    this->assign(this->val + val);
    return *this;
  }
  modllint& operator+=(const modllint& obj){
    this->assign(this->val + obj.val);
    return *this;
  }
  modllint operator+(const llint val){return this += val;}
  modllint operator+(const modllint& obj){return this += obj;}
  
  modllint& operator*=(const llint val){
    this->assign(this->val * val);
    return *this;
  }
  modllint& operator*=(const modllint& obj){
    this->assign(this->val * obj.val);
    return *this;
  }
  modllint operator*(const llint val){return this *= val;}
  modllint operator*(const modllint& obj){return this *= obj;}

  modllint& operator=(const modllint& obj){
    assign(obj.val);
    return *this;
  }
  modllint& operator=(const llint intvalue){
    assign(intvalue);
    return *this;
  }
  
  friend std::ostream& operator<<(std::ostream& os, const modllint<MODVALUE>& val){
    os << val.val;
    return os;
  }
  friend std::istream& operator>>(std::istream& is, modllint<MODVALUE>& val){
    is >> val.val;
    return is;
  }
};



int main(void){
  llint t1, t2, a1, a2, b1, b2;
  std::cin >> t1 >> t2 >> a1 >> a2 >> b1 >> b2;

  if(a1*t1 + a2*t2 == b1*t1 + b2*t2){
    std::cout << "infinity" << std::endl;
    return 0;
  }

  llint c1a = t1 * a1;
  llint c2a = c1a + t2*a2;
  llint c1b = t1*b1;
  llint c2b = c1b + t2*b2;
  
  if( ((c1a-c1b)>=0) ^ ((c2a-c2b)<0)){
    std::cout << 0 << std::endl;
    return 0;
  }

  llint minus_xmin = -100000000000000000L;
  llint minus_xmax = 0L;
  llint plus_xmin = 0L;
  llint plus_xmax = 100000000000000000L;
  while(minus_xmax - minus_xmin > 1){
    llint mid = (minus_xmin + minus_xmax)/2L;
    if( ((c1a-c1b + mid)<0) ^((c2a-c2b + mid)<0) ){
      // de a u yo----
      minus_xmax = mid;
    }else{
      minus_xmin = mid;
    }
  } 
  while(plus_xmax - plus_xmin > 1){
    llint mid = (plus_xmin + plus_xmax)/2L;
    if( ((c1a-c1b + mid)<0) ^((c2a-c2b + mid)<0) ){
      // de a u yo----
      plus_xmin = mid;
    }else{
      plus_xmax = mid;
    }
  }
  llint minus, plus;
  if( ((c1a-c1b + minus_xmin)<=0) ^((c2a-c2b + minus_xmin)<=0) ) minus = minus_xmin;
  else                                                         minus = minus_xmax;
  if( ((c1a-c1b + plus_xmax)<=0) ^((c2a-c2b + plus_xmax)<=0) ) plus = plus_xmax;
  else                                                       plus = plus_xmin;
  //std::cout << minus_xmax << " " << minus_xmin << std::endl;
  //std::cout << plus_xmax << " " << plus_xmin << std::endl;
  //std::cout << minus << " <= x <= " << plus << std::endl;
  
  llint diff = c2a - c2b;
  //std::cout << diff << std::endl;

  if(diff > 0){
    int temp = plus%diff == 0;
    std::cout << plus/diff*2 + 1L -temp << std::endl;
    
  }else{
    int temp = (-minus)%(-diff) == 0;
    std::cout << (-minus)/(-diff)*2 + 1L -temp<< std::endl;
  }
  
  return 0;
}