#    X2 
# X1 X0 X4 X5 
#    X3
#
# C0    C1
#    X0 
# C3    C2
#
# C1    C0
#    5X 
# C2    C3
#
N = int(raw_input())
# N = 6
# N = 8

C_list = []
for n in range(N):
    C_list.append(map(int, raw_input().split()))


"""
C_list = [
    [0, 1, 2, 3], 
    [0, 4, 6, 1], 
    [1, 6, 7, 2], 
    [2, 7, 5, 3], 
    [6, 4, 5, 7], 
    [4, 0, 3, 5]
]

C_list = [
    [0, 0, 0, 0], 
    [0, 0, 1, 1], 
    [0, 1, 0, 1], 
    [0, 1, 1, 0], 
    [1, 0, 0, 1], 
    [1, 0, 1, 0], 
    [1, 1, 0, 0], 
    [1, 1, 1, 1]
]

C_list = [
    [0, 0, 0, 0], 
    [0, 0, 0, 0], 
    [0, 0, 0, 0], 
    [0, 0, 0, 0], 
    [0, 0, 0, 0], 
    [0, 0, 0, 0]
]
"""
res = 0

def rotate(A, n):
    if n == 0:
        return A
    elif n == 1:
        return [A[1], A[2], A[3], A[0]]
    elif n == 2:
        return [A[2], A[3], A[0], A[1]]
    elif n == 3:
        return [A[3], A[0], A[1], A[2]]
    

C_list_1 = [rotate(C, 1) for C in C_list]
C_list_2 = [rotate(C, 2) for C in C_list]
C_list_3 = [rotate(C, 3) for C in C_list]


for i in range(N-1):
    X0_ = C_list[i]
    for j in range(i+1, N):
        X5 = [C_list[j][1], C_list[j][0], C_list[j][3], C_list[j][2]]
        for k in range(4):
            X0 = rotate(X0_, k)
            X1 = [X5[0], X5[1], X0[1], X0[0]]
            X2 = [X5[1], X5[2], X0[2], X0[1]]
            X3 = [X5[2], X5[3], X0[3], X0[2]]
            X4 = [X5[3], X5[0], X0[0], X0[3]]

            # drop duplicate
            c1 = 0
            c2 = 0
            c3 = 0
            c4 = 0
            
            if X2 in [rotate(X1, l) for l in range(4)]:
                c2 += 1
            if X3 in [rotate(X1, l) for l in range(4)]:
                c3 += 1
            if X3 in [rotate(X2, l) for l in range(4)]:
                c3 += 1
            if X4 in [rotate(X1, l) for l in range(4)]:
                c4 += 1
            if X4 in [rotate(X2, l) for l in range(4)]:
                c4 += 1
            if X4 in [rotate(X3, l) for l in range(4)]:
                c4 += 1
            
            # count how many fits
            r1 = 0
            r1 += max(sum([C_list[m] == X1 for m in range(i+1, N) if m != j]) - c1, 0)
            r1 += max(sum([C_list_1[m] == X1 for m in range(i+1, N) if m != j]) - c1, 0)
            r1 += max(sum([C_list_2[m] == X1 for m in range(i+1, N) if m != j]) - c1, 0)
            r1 += max(sum([C_list_3[m] == X1 for m in range(i+1, N) if m != j]) - c1, 0)
            r2 = 0
            r2 += max(sum([C_list[m] == X2 for m in range(i+1, N) if m != j]) - c2, 0)
            r2 += max(sum([C_list_1[m] == X2 for m in range(i+1, N) if m != j]) - c2, 0)
            r2 += max(sum([C_list_2[m] == X2 for m in range(i+1, N) if m != j]) - c2, 0)
            r2 += max(sum([C_list_3[m] == X2 for m in range(i+1, N) if m != j]) - c2, 0)
            r3 = 0
            r3 += max(sum([C_list[m] == X3 for m in range(i+1, N) if m != j]) - c3, 0)
            r3 += max(sum([C_list_1[m] == X3 for m in range(i+1, N) if m != j]) - c3, 0)
            r3 += max(sum([C_list_2[m] == X3 for m in range(i+1, N) if m != j]) - c3, 0)
            r3 += max(sum([C_list_3[m] == X3 for m in range(i+1, N) if m != j]) - c3, 0)
            r4 = 0
            r4 += max(sum([C_list[m] == X4 for m in range(i+1, N) if m != j]) - c4, 0)
            r4 += max(sum([C_list_1[m] == X4 for m in range(i+1, N) if m != j]) - c4, 0)
            r4 += max(sum([C_list_2[m] == X4 for m in range(i+1, N) if m != j]) - c4, 0)
            r4 += max(sum([C_list_3[m] == X4 for m in range(i+1, N) if m != j]) - c4, 0)
            res += r1*r2*r3*r4
            
print res

### FIXED CODE ###

N = int(raw_input())
# T_list = []
# A_list = []
T = 1
A = 1

for n in range(N):
    t, a = map(int, raw_input().split())
    k = max(int(T/t), int(A/a))
    if T > t*k or A > a*k:
        T = t*(k+1)
        A = a*(k+1)
    else:
        T = t*k
        A = a*k

print T+A