from fractions import gcd
from functools import reduce

def lcm(a,b):

    return a*b/gcd(a,b)

while True:

    tmp = list(map(int, input().split(" ")))

    if all(t==0 for t in tmp):

        break

    b = []

    for a,m in zip(tmp[::2], tmp[1::2]):

        i, c = 1, a%m

        while c!=1:

            c = (a*c)%m

            i += 1

        b.append(i)

    print(reduce(lcm,b))
from fractions import gcd
from functools import reduce

def lcm(a,b):

    return a*b/gcd(a,b)

def ge(a,m):

    for aa,mm in zip(a, m): 

        i, b = 1, aa%mm

        while b!=1:

            b = (aa*b)%mm

            i += 1

        yield i

while True:

    tmp = list(map(int, input().split(" ")))

    if all(t==0 for t in tmp):

        break

    print(reduce(lcm,ge(tmp[::2],tmp[1::2])))
from fractions import gcd
from functools import reduce


def lcm(a, b):
    return a * b / gcd(a, b)


while True:
    tmp = list(map(int, input().split(" ")))
    if all(t == 0 for t in tmp):
        break
    b = []
    for a, m in zip(tmp[::2], tmp[1::2]):
        i, c = 1, a % m
        while c != 1:
            c = (a * c) % m
            i += 1
        b.append(i)
    print(reduce(lcm, b))

from fractions import gcd
from functools import reduce


def lcm(a, b):
    return a * b / gcd(a, b)


def ge(a, m):
    for aa, mm in zip(a, m):
        i, b = 1, aa % mm
        while b != 1:
            b = (aa * b) % mm
            i += 1
        yield i


while True:
    tmp = list(map(int, input().split(" ")))
    if all(t == 0 for t in tmp):
        break
    print(reduce(lcm, ge(tmp[::2], tmp[1::2])))

+def ge(a, m): +    for aa, mm in zip(a, m): +        i, b = 1, aa % mm +        while b != 1: +            b = (aa * b) % mm +            i += 1 +        yield i + + -    b = [] -    for a, m in zip(tmp[::2], tmp[1::2]): -        i, c = 1, a % m -        while c != 1: -            c = (a * c) % m -            i += 1 -        b.append(i) -    print(reduce(lcm, b)) +    print(reduce(lcm, ge(tmp[::2], tmp[1::2])))