import java.io.*; import java.util.*; import java.util.stream.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int k = sc.nextInt(); int p = 0; HashMap org = new HashMap<>(); for (int i = 0; i < k; i++) { int x = sc.nextInt(); int e = sc.nextInt(); p = x; org.put(x, e); } int[] values = new int[p + 1]; ArrayList> all = new ArrayList<>(); for (int i = 0; i <= p; i++) { values[i] = i; all.add(new HashMap<>()); } for (int i = 2; i <= p; i++) { if (values[i] > 1) { all.get(i).put(i, all.get(i).getOrDefault(i, 0) + 1); for (int j = 2; j * i <= p; j++) { while (values[j * i] % i == 0) { all.get(j * i).put(i, all.get(j * i).getOrDefault(i, 0) + 1); values[j * i] /= i; } } } for (int x : all.get(i - 1).keySet()) { all.get(i).put(x, all.get(i).getOrDefault(x, 0) + all.get(i - 1).get(x)); } } TreeMap sum = new TreeMap<>(); for (int x : all.get(p).keySet()) { sum.put(x, all.get(p).get(x) - org.getOrDefault(x, 0)); if (sum.get(x) == 0) { sum.remove(x); } else if (sum.get(x) < 0) { System.out.println("-1 -1"); return; } } int start = sum.lastKey(); for (int i = start; i < p; i++) { if (i > start && isPrime(i)) { break; } int x = p - i; int y; if (i < x) { y = i; } else { y = x; x = i; } boolean enable = true; for (int z : all.get(x).keySet()) { enable = (all.get(x).get(z) + all.get(y).getOrDefault(z, 0) == sum.getOrDefault(z, 0)); if (!enable) { break; } } if (enable) { System.out.println(p + " " + y); return; } } System.out.println("-1 -1"); } static boolean isPrime(int x) { for (int i = 2; i <= Math.sqrt(x); i++) { if (x % i == 0) { return false; } } return true; } } class Utilities { static String arrayToLineString(Object[] arr) { return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n")); } static String arrayToLineString(int[] arr) { return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new)); } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public int[] nextIntArray() throws Exception { return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }