#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<vector>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<time.h>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
const int inf=1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
int main(){
	int in[6]={0};
	while(cin>>in[0]>>in[1]>>in[2]>>in[3]>>in[4]>>in[5],in[0]+in[1]+in[2]+in[3]+in[4]+in[5]){
	rep(i,3){
		int a=in[i*2],b=in[i*2+1];
		int co=0;
		int d=1;
		while(1){
			co++;
			d=(a*d)%b;
			if(d==1)break;
		}
		in[i]=co;
	}
	cout<<__gcd(in[0],__gcd(in[1],in[2]))<<endl;
}}