Training Site
Sideways banner

Supply Scheduling

Input: Standard Input (stdin)
Output: Standard Output (stdout)
Memory limit: 128 megabytes
Time limit: 2.0 seconds

Two new zoos are set to open on the streets of New Zealand: Aardvark Asylum, and Basilisk Biosphere. When a zoo orders animal food, they have to pay for the shipping and the food. The shipping is a fixed cost, no matter how much animal food they order, and all shipments arrive on the day they're ordered. The two zoos realised that if they ordered food on the same day, they would only have to pay the shipping cost once between the two of them, saving them money!

Unfortunately, both zoos require food shipments according to their own schedule. They both require shipments on exactly N days. Aardvark Asylum's schedule, A, is made up of N integers. Each integer in A represents an amount of days after opening, when the Asylum will require a shipment of food. Basilisk Biosphere's schedule, B, is also made up of N integers, each of which represents an amount of days after opening, when the Biosphere will require a shipment of food.

More formally, the Asylum requires a shipment of food A_i days after opening, for all values of i such that (0 ≤ i ≤ N - 1), and the Biosphere requires a shipment of food B_i days after opening, for all values of i such that (0 ≤ i ≤ N - 1). Neither zoo can change their schedule, but they can change which day they open on. You have been brought on as a consultant, to help them figure out the fewest possible amount of days on which either zoo needs to order food, if the opening days of the two zoos are staggered optimally.

Input

The first line of input contains an integer N.

The second line of input contains N space-separated integers A, the ith of which denotes that Aardvark Asylum needs to order food A_i days after opening. It is guaranteed that these will be given in ascending order.

The third line of input contains N space-separated integers B, the ith of which denotes that Basilisk Biosphere needs to order food B_i days after opening. It is guaranteed that these will also be given in ascending order.

Output

Output a single integer - the fewest achievable number of days on which either zoo would need to order food, if the opening days of the two zoos are staggered optimally.

Constraints

  • 1 ≤ N ≤ 1{,}000
  • 1 ≤ A_i, B_i ≤ 1{,}000{,}000{,}000 for all (0 ≤ i ≤ N - 1)
  • A_i > A_{i - 1} and B_i > B_{i - 1} for all (1 ≤ i ≤ N - 1)

Subtasks

  • Subtask 1 (+20%): N = 2
  • Subtask 2 (+25%): A_i ≤ 2{,}000 and B_i ≤ 2{,}000 for all (0 ≤ i ≤ N - 1)
  • Subtask 3 (+30%): N ≤ 100
  • Subtask 4 (+25%): No further constraints.

Sample Explanation

By making Basilisk Biosphere open a day before Aardvark Asylum, the Biosphere's 2nd, 4th, and 5th shipments will coincide with the Asylum's 1st, 4th, and 5th shipments respectively, meaning that only 7 individual shipments need to be made.

NOTE

If you are using Python, select Python 3.6 (PyPy 7.3) among the language options when making a submission. Otherwise, your solution may not pass the time limit for some subtasks even if it is maximally efficient.

  • Sample Input 1

    5
    2 3 6 8 10
    1 3 8 9 11
    

    Sample Output 1

    7