#include<stdio.h>
#include<algorithm>
#include<set>
using namespace std;
int gcd(int a,int b){
	while(a){
		b%=a;
		int c=a;a=b;b=c;
	}
	return b;
}
set<pair<int,int> > S;
set<pair<int,int> > T;
int main(){
	int a,b,c,d,e,f;
	scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f);
	int row=0;
	int col=0;
	long long cnt=1;
	S.insert(make_pair(row,col));
	while(1){
		row=(row+c)%a;
		col=(col+d)%b;
		if(col==0)break;
		cnt++;
		S.insert(make_pair(row,col));
	}
	int g=gcd(row,a);
	long long ret=a/g*cnt;
	if(ret<110000){
		S.clear();
		swap(c,e);
		swap(d,f);
		row=0;col=0;cnt=1;
		S.insert(make_pair(row,col));
		while(1){
			row=(row+c)%a;
			col=(col+d)%b;
			if(col==0)break;
			cnt++;
			S.insert(make_pair(row,col));
		}
		g=gcd(row,a);
		ret=a/g*cnt;
	}
	for(set<pair<int,int> > ::iterator it=S.begin();it!=S.end();it++){
		pair<int,int>tmp=(*it);
		T.insert(make_pair(tmp.first%g,tmp.second));
	}
	//printf("%lld\n",ret);
	row=0;col=0;cnt=1;
	  while(1){
        row=(row+e)%a;
        col=(col+f)%b;
        if(T.count(make_pair(row%g,col)))break;
        cnt++;
    }
	//printf("%lld %lld\n",ret,cnt);
	printf("%lld\n",ret*cnt-1);
}
### Prompt

Construct a Cpp code solution to the problem outlined:

There is a board of m × n squares. The squares of i rows and j columns are represented by (i, j) (0 ≤ i <m, 0 ≤ j <n).

When the rabbit is at (x, y), it can jump to ((x + a) mod m, (y + b) mod n) or ((x + c) mod m, (y + d) mod n) it can.

Now the rabbit is at (0, 0). If you can't go back to the square that once jumped, how many times can the rabbit jump?



Input

Input is given with m, n, a, b, c, d separated by spaces on one line. 1 ≤ m, n, a, b, c, d ≤ 100 000

Output

Output the maximum number of times the rabbit can jump in one line.

Example

Input

6 6 2 2 2 4


Output

8

### Response

```cpp
#include<stdio.h>
#include<algorithm>
#include<set>
using namespace std;
int gcd(int a,int b){
	while(a){
		b%=a;
		int c=a;a=b;b=c;
	}
	return b;
}
set<pair<int,int> > S;
set<pair<int,int> > T;
int main(){
	int a,b,c,d,e,f;
	scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&e,&f);
	int row=0;
	int col=0;
	long long cnt=1;
	S.insert(make_pair(row,col));
	while(1){
		row=(row+c)%a;
		col=(col+d)%b;
		if(col==0)break;
		cnt++;
		S.insert(make_pair(row,col));
	}
	int g=gcd(row,a);
	long long ret=a/g*cnt;
	if(ret<110000){
		S.clear();
		swap(c,e);
		swap(d,f);
		row=0;col=0;cnt=1;
		S.insert(make_pair(row,col));
		while(1){
			row=(row+c)%a;
			col=(col+d)%b;
			if(col==0)break;
			cnt++;
			S.insert(make_pair(row,col));
		}
		g=gcd(row,a);
		ret=a/g*cnt;
	}
	for(set<pair<int,int> > ::iterator it=S.begin();it!=S.end();it++){
		pair<int,int>tmp=(*it);
		T.insert(make_pair(tmp.first%g,tmp.second));
	}
	//printf("%lld\n",ret);
	row=0;col=0;cnt=1;
	  while(1){
        row=(row+e)%a;
        col=(col+f)%b;
        if(T.count(make_pair(row%g,col)))break;
        cnt++;
    }
	//printf("%lld %lld\n",ret,cnt);
	printf("%lld\n",ret*cnt-1);
}
```