This page is still under construction.

Parts of this page are still being built. What you see may change.

Chinese Remainder Theorem

Time limit1sMemory limit256 MB

Summary
Given pairs (a_i, b_i), find the largest m such that a_i is congruent to b_i modulo m for every i; since a_i >= b_i, m must divide all differences a_i - b_i.
Level

Medium6 of 10

Topics
Number theory, Math, Greedy, Implementation
Solved
No attempts yet

Problem

Johnny is a computer science student. This semester he became well versed with the Chinese Remainder Theorem. While waiting for a next lecture he heard that Maggie complained that she cannot solve her homework; as he heard the familiar words "modulo" and "system of equations" he immediately offered his help to the damsel in distress. It turns out that Maggie's task is much different than those that Johnny is accustomed to solve, it is of the following form:

{a1≡b1(modm)a2≡b2(modm)⋮⋮⋮an≡bn(modm)\left\{\begin{array}{ccc} a_1 & \equiv & b_1 \pmod{m} \\ a_2 & \equiv & b_2 \pmod{m} \\ \vdots & \vdots & \vdots \\ a_n & \equiv & b_n \pmod{m} \end{array}\right.

(where ≡\equiv means equivalence modulo) and for the given a1,b1,…,an,bna_1, b_1, \dots, a_n, b_n Maggie should compute the largest mm such that all of the equations hold. Maggie already started processing the equations and she ensured that ai≥bia_i \geq b_i for each ii. Johnny cannot fail and lose his face. Help him to solve the task.

Input

The first line of the input contains a single integer nn (1≤n≤1051 \leq n \leq 10^5), denoting the number of equations.

The second line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n, each separated by a single space, these are the numbers on the left-hand sides of consecutive equations.

The third and last line contains nn integers b1,b2,…,bnb_1, b_2, \dots, b_n, each separated by a single space, these are the numbers on the right-hand sides of consecutive equations.

The inequality 0≤bi≤ai≤10180 \leq b_i \leq a_i \leq 10^{18} holds for each ii (1≤i≤n1 \leq i \leq n). The system of equations is nontrivial: ai≠bia_i \neq b_i holds for some ii (1≤i≤n1 \leq i \leq n).

Output

You should write a single integer in the first and only line of the output: the largest mm for which the given system of equations is satisfied.

Hint

For Sample 1, the system of equations {7≡3(mod4)17≡5(mod4)9≡1(mod4)\left\{\begin{array}{ccc} 7 & \equiv & 3 \pmod{4} \\ 17 & \equiv & 5 \pmod{4} \\ 9 & \equiv & 1 \pmod{4} \end{array}\right. is satisfied and it is easy to verify that it is not satisfied for m>4m > 4.

For Sample 2, the system of equations {4≡2(mod1)6≡2(mod1)5≡2(mod1)\left\{\begin{array}{ccc} 4 & \equiv & 2 \pmod{1} \\ 6 & \equiv & 2 \pmod{1} \\ 5 & \equiv & 2 \pmod{1} \end{array}\right. is satisfied and it is easy to verify that it is not satisfied for m>1m > 1.

Examples2

  1. Example 1

    Input
    3
    7 17 9
    3 5 1
    
    Expected output
    4
    
  2. Example 2

    Input
    3
    4 6 5
    2 2 2
    
    Expected output
    1