This page is still under construction.

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

N-orthotope

Interview

Time limit1sMemory limit256 MB

Summary
Given two axis-aligned boxes in N dimensions, print the dimension of their overlap, or -1 when they do not meet.
Level

Easy1 of 10

Topics
Implementation, Geometry
Solved
No attempts yet

Problem

An axis parallel N-orthotope is the set of N dimensional points written in this form.

[s1,e1]×[s2,e2]×⋯×[sN,eN](si<ei)[s_1, e_1] \times [s_2, e_2] \times \cdots \times [s_N, e_N] \quad (s_i < e_i)

That is, it collects every point (x1,x2,…,xN)(x_1, x_2, \ldots, x_N) whose coordinate xix_i satisfies si≤xi≤eis_i \le x_i \le e_i.

  • For N=0N = 0 the set is defined as a point in 0 dimensions.
  • For N=1N = 1 it is a segment on a line.
  • For N=2N = 2 it is an axis parallel rectangular region in the plane.
  • For N=3N = 3 it is an axis parallel rectangular box in space.

Generalize a little and allow si≤eis_i \le e_i in the product

[s1,e1]×[s2,e2]×⋯×[sK,eK](si≤ei)[s_1, e_1] \times [s_2, e_2] \times \cdots \times [s_K, e_K] \quad (s_i \le e_i)

If exactly NN indices satisfy si<eis_i < e_i, this set of points is an axis parallel N-orthotope. The words 'axis parallel' are dropped from here on, but every orthotope below is still axis parallel.

Two N-orthotopes are given for some NN. If some points belong to both regions at once, those points form an M-orthotope. Write a program that computes MM. For 2-orthotopes there are the four cases below.

  • In A the common region is again a 2-orthotope.
  • In B the common region is a 1-orthotope, a segment.
  • In C the common region is a 0-orthotope, a point.
  • In D there is no common region. Print -1 in that case.

Input

The first line has a natural number NN.

The second line has s1,e1,s2,e2,…,sN,eNs_1, e_1, s_2, e_2, \ldots, s_N, e_N of the first region, separated by spaces.

The third line has s1,e1,s2,e2,…,sN,eNs_1, e_1, s_2, e_2, \ldots, s_N, e_N of the second region, separated by spaces.

Every given number is an integer with absolute value at most 11, and si<eis_i < e_i holds for each ii.

1≤N≤111 \le N \le 11.

Output

On the first line print MM if the common region of the two regions is an M-orthotope. If there is no common region, print -1.

Examples5

  1. Example 1

    Input
    2
    0 5 0 5
    2 9 2 9
    
    Expected output
    2
    
  2. Example 2

    Input
    2
    0 5 0 5
    5 10 5 10
    
    Expected output
    0
    
  3. Example 3

    Input
    2
    0 5 0 5
    5 10 2 9
    
    Expected output
    1
    
  4. Example 4

    Input
    2
    0 5 0 5
    6 10 0 5
    
    Expected output
    -1
    
  5. Example 5

    Input
    1
    -11 11
    -5 0
    
    Expected output
    1