import scala.io.StdIn.{readInt,readLine}

object Main {

  def gcd(x:Long, y:Long):Long = if(y==0) x else gcd(y, x%y)
  def lcm(x:Long, y:Long) = (x/gcd(x,y))*y
        def pow(x:Long, y:Long, z:Long):Long = {
                def multi(m:Long, n:Long) = { ( (m%z) * (n%z) ) % z }

                def scan(m:Long, n:Long, odd:Long, b:Long):Long = {
                        n match {
                                case 0 => 1
                                case 1 => multi(m, odd)
                                case _ =>
                                        scan(multi(m,m), n/2, (if(n%2==1) multi(odd, b) else odd), multi(b,b))
                        }
                }
                scan(x, y, 1, x)
        }

  def main(args:Array[String]) = {
    Iterator.continually(readLine).takeWhile(_.trim!="0 0 0 0 0 0").foreach { s =>
      val n = s.split(" ").map(_.toLong)
      val a1 = n(0);val m1 = n(1);val a2 = n(2);val m2 = n(3);val a3 = n(4);val m3 = n(5)
      val i1 = Stream.from(1).find(pow(a1,_,m1)==1).getOrElse(0)
      val i2 = Stream.from(1).find(pow(a2,_,m2)==1).getOrElse(0)
      val i3 = Stream.from(1).find(pow(a3,_,m3)==1).getOrElse(0)
      println(lcm(i1,lcm(i2,i3)))
    }
  }
}