#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);
}