#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>
#include <functional>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define rep1(i,n) for(int i=1;i<=(n);++i)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl;
typedef long long ll;
ll gcd(ll x,ll y){
	if(x>y) swap(x,y);
	while(x>0){
		y%=x;
		swap(x,y);
	}
	return y;
}
ll lcm(ll x,ll y){
	return x/gcd(x,y)*y;
}
vector<ll> divs;
ll ex(ll x,ll p,ll mod){
	ll ret=1;
	while(p>0){
		if(p%2) ret=x*ret%mod;
		x=x*x%mod;
		p/=2;
	}
	return ret;
}
ll extgcd(ll a,ll b,ll &x,ll &y){
	if(b==0){
		x=a,y=0;
		return a;
	}
	ll d=extgcd(b,a%b,x,y),xx=y,yy=x-a/b*y;
	x=xx,y=yy;
	return d;
}
ll inv(ll a,ll mod){
	ll x,y;
	extgcd(a,mod,x,y);
	return (x%mod+mod)%mod;
}
int main(){
	ll m,n,a,b,c,d;
	cin>>m>>n>>a>>b>>c>>d;
	ll x=lcm(m/gcd(m,a),n/gcd(n,b));
	for(ll i=1;i*i<=m*n;i++) if(m*n%i==0) divs.pb(i),divs.pb(m*n/i);
	sort(all(divs));
	for(ll y:divs){
		ll p=y*c;
		ll g=gcd(a,m);
		if(p%g>0) continue;
		ll pc=p/g,ac=a/g,mc=m/g;
		ll e=pc*inv(ac,mc);
		ll q=y*d;
		g=gcd(b,n);
		if(q%g>0) continue;
		ll qc=q/g,bc=b/g,nc=n/g;
		ll f=qc*inv(bc,nc);
		g=gcd(mc,nc);
		if(e%g==f%g){
			cout<<x*y-1<<endl;
			break;
		}
	}
}
### Prompt

Your challenge is to write a cpp solution to the following problem:

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 <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>
#include <functional>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define rep1(i,n) for(int i=1;i<=(n);++i)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl;
typedef long long ll;
ll gcd(ll x,ll y){
	if(x>y) swap(x,y);
	while(x>0){
		y%=x;
		swap(x,y);
	}
	return y;
}
ll lcm(ll x,ll y){
	return x/gcd(x,y)*y;
}
vector<ll> divs;
ll ex(ll x,ll p,ll mod){
	ll ret=1;
	while(p>0){
		if(p%2) ret=x*ret%mod;
		x=x*x%mod;
		p/=2;
	}
	return ret;
}
ll extgcd(ll a,ll b,ll &x,ll &y){
	if(b==0){
		x=a,y=0;
		return a;
	}
	ll d=extgcd(b,a%b,x,y),xx=y,yy=x-a/b*y;
	x=xx,y=yy;
	return d;
}
ll inv(ll a,ll mod){
	ll x,y;
	extgcd(a,mod,x,y);
	return (x%mod+mod)%mod;
}
int main(){
	ll m,n,a,b,c,d;
	cin>>m>>n>>a>>b>>c>>d;
	ll x=lcm(m/gcd(m,a),n/gcd(n,b));
	for(ll i=1;i*i<=m*n;i++) if(m*n%i==0) divs.pb(i),divs.pb(m*n/i);
	sort(all(divs));
	for(ll y:divs){
		ll p=y*c;
		ll g=gcd(a,m);
		if(p%g>0) continue;
		ll pc=p/g,ac=a/g,mc=m/g;
		ll e=pc*inv(ac,mc);
		ll q=y*d;
		g=gcd(b,n);
		if(q%g>0) continue;
		ll qc=q/g,bc=b/g,nc=n/g;
		ll f=qc*inv(bc,nc);
		g=gcd(mc,nc);
		if(e%g==f%g){
			cout<<x*y-1<<endl;
			break;
		}
	}
}
```