def f(a,b):

  p,q=a,b

  while q!=0: p,q=q,p%q

  return a/p*b

  

while 1:

  x=list(map(int,input().split()))

  if any(x)==0: break

  A=[]

  for i in range(0,6,2):

    a,b=x[i:i+2]

    c=1

    for j in range(1,b):

      c=(c*a)%b

      if c==1: break

    A.append(j)

  a,b,c=A

  print(f(f(a,b),c))
def f(a,b):

  p,q=a,b

  while q!=0: p,q=q,p%q

  return a/p*b



def g(a,b):

  c,i=a%b,1

  while c!=1: i,c=i+1,(c*a)%b

  return i

  

while 1:

  x=list(map(int,input().split()))

  if any(x)==0: break

  a,b,c=[g(x[i],x[i+1]) for i in range(0,6,2)]

  print(f(f(a,b),c))
def f(a, b):
    p, q = a, b
    while q != 0:
        p, q = q, p % q
    return a / p * b


while 1:
    x = list(map(int, input().split()))
    if any(x) == 0:
        break
    A = []
    for i in range(0, 6, 2):
        a, b = x[i : i + 2]
        c = 1
        for j in range(1, b):
            c = (c * a) % b
            if c == 1:
                break
        A.append(j)
    a, b, c = A
    print(f(f(a, b), c))

def f(a, b):
    p, q = a, b
    while q != 0:
        p, q = q, p % q
    return a / p * b


def g(a, b):
    c, i = a % b, 1
    while c != 1:
        i, c = i + 1, (c * a) % b
    return i


while 1:
    x = list(map(int, input().split()))
    if any(x) == 0:
        break
    a, b, c = [g(x[i], x[i + 1]) for i in range(0, 6, 2)]
    print(f(f(a, b), c))

+def g(a, b): +    c, i = a % b, 1 +    while c != 1: +        i, c = i + 1, (c * a) % b +    return i + + -    A = [] -    for i in range(0, 6, 2): -        a, b = x[i : i + 2] -        c = 1 -        for j in range(1, b): -            c = (c * a) % b -            if c == 1: -                break -        A.append(j) -    a, b, c = A +    a, b, c = [g(x[i], x[i + 1]) for i in range(0, 6, 2)]