Under construction

Still being built. Things may change

Covering an Interval

Time limit2sMemory limit1024 MB

Summary
Given N segments on a line, pick at most three that cover the interval from S to E, minimizing the total length of overlaps between the chosen pairs.
Level

Medium6 of 10

Topics
Intervals, Sorting, Dynamic programming
Solved
No attempts yet

Problem

There are NN segments on a one-dimensional line. You want to cover the interval from x=Sx = S to x=Ex = E using at most 3 of the given segments.

A segment with start point x=lx=l and end point x=rx=r covers a point xx when l≤x≤rl \leq x \leq r. A set of segments LL can cover the interval when, for every real number xx with S≤x≤ES \leq x \leq E, at least one segment in LL covers xx.

The 'error' of a covering is the sum of the lengths of the overlaps between every pair of two different segments used. If one segment alone covers the whole interval from x=Sx = S to x=Ex = E, the error of that covering is 0.

Find the minimum possible 'error' over all coverings that use at most 3 segments.

Input

The first line contains the number of segments NN, the start point SS, and the end point EE, separated by spaces.

The next NN lines contain the start point sis_i and the end point eie_i of the ii-th segment, separated by spaces.

Output

Print the minimum possible error on the first line.

If the interval from x=Sx=S to x=Ex=E cannot be covered in any way, print −1-1.

Limits

  • 1≤N≤1,000,0001 \leq N \leq 1,000,000
  • 1≤si<ei≤1091 \leq s_i < e_i \leq 10^9
  • 1≤S<E≤1091 \leq S < E \leq 10^9
  • All numbers in the input are integers.

Examples2

  1. Example 1

    Input
    3 1 10
    1 7
    2 10
    3 6
    
    Expected output
    5
    
  2. Example 2

    Input
    5 14 46
    1 16
    32 45
    39 48
    42 47
    36 46
    
    Expected output
    -1