#include<iostream>
#include<algorithm>

using namespace std;

int gcd(int a,int b){
  if(b==0) return a;
  return gcd(b,a%b);
}
 
int main(){

  int a[3],m[3];
  long long c[3];
  int n;
  long long ans;

  while(1){

    cin>>a[0]>>m[0]>>a[1]>>m[1]>>a[2]>>m[2];
    if(a[0]==0) break;

    for(int i=0;i<3;i++){
      n=(1*a[i])%m[i];
      c[i]=1;
      while(1){
	if(n==1) break;
	c[i]++;
	n=(n*a[i])%m[i];
      }
      cout<<c[i]<<","<<endl;
    }

    ans=c[0];
    ans=(ans*c[1])/(gcd(max(c[1],ans),min(c[1],ans)));
    ans=(ans*c[2])/gcd(max(c[2],ans),min(c[2],ans));

    cout<<ans<<endl;
  }

}