from math import log, lgamma def deg(n, p): ret = 0 n //= p while n: ret += n n //= p return ret def isqrt(n): if n <= 0: return 0 x = int((n ** 0.5) * (1 + 1e-14)) while True: y = (x + n // x) // 2 if y >= x: return x x = y k = int(input()) pe = [] for _ in range(k): pe.append([int(x) for x in input().split()]) P = pe[-1][0] P1 = P + 1 needlog = 0.0 for p, e in pe: if e > deg(P, p): print(-1, -1) exit needlog += log(p) * e lgP = lgamma(P1) lo1, hi1 = 1, P // 2 if lgP - lgamma(hi1) - lgamma(P1 - hi1) - needlog < -1e-6: print(-1, -1) exit while hi1 - lo1 > 1: md = lo1 + (hi1 - lo1) // 2 lgcomb = lgP - lgamma(md + 1) - lgamma(P1 - md) if lgcomb - needlog < -1e-6: lo1 = md else: hi1 = md lo2, hi2 = 1, P // 2 while hi2 - lo2 > 1: md = lo2 + (hi2 - lo2) // 2 lgcomb = lgP - lgamma(md + 1) - lgamma(P1 - md) if lgcomb - needlog > 1e-6: hi2 = md else: lo2 = md for r in range(lo1, hi2 + 1): if all(deg(P, p) - deg(r, p) - deg(P - r, p) == e for p, e in pe): print(P, r) exit else: print(-1, -1)