#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int calc(int a,int m, int pos){return a*pos%m;}
ll calc(ll a,ll b){return (a/__gcd(a,b))*b;}

int main(){
  int a1,m1,a2,m2,a3,m3;
  while(1){
  cin>>a1>>m1>>a2>>m2>>a3>>m3;
  if(!a1)break;
  ll x=1,X=1;
  ll y=1,Y=1;
  ll z=1,Z=1;
  while((x=calc(a1,m1,x))!=1)X++;
  while((y=calc(a2,m2,y))!=1)Y++;
  while((z=calc(a3,m3,z))!=1)Z++;
  cout <<calc(calc(X,Y),Z)<<endl;
  }
  return 0;
}