#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <map>
#include <set>
#include <cstdio>
#include <cmath>
#define rep(i,l,n) for(lint i=l;i<n;i++)
#define rer(i,l,n) for(lint i=l;i<=n;i++)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define pb(a) push_back(a)
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef vector<int> vi;
typedef vector<lint> vli;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

lint gcd(lint a,lint b){
	if(b==0) return a;
	return gcd(b,a%b);
}
lint mini(lint a,lint b){
	lint g=gcd(a,b);
	return a*b/g;
}
lint solve(lint a,lint m){
	lint now=1,res=0;
	for(res=1;;res++){
		now=(a*now)%m;
		if(now==1) return res;
	}
}
int main(){
	lint a,b,c,l,m,n;
	while(cin>>a>>l>>b>>m>>c>>n){
		if(a==0 && b==0 && c==0 && l==0 && m==0 && n==0) break;
		lint ans=0,A,B,C;
		A=solve(a,l);
		B=solve(b,m);
		C=solve(c,n);
		ans=mini(mini(A,B),C);
		o(ans);
	}		
}