Under construction

Still being built. Things may change

Events are coming (Easy)

Interview

Time limit1sMemory limit1024 MB

Summary
Given times T with forbidden open intervals (A, B), find the minimum distance Minjeong walks from 0 at speed at most 1 to avoid them, or report -1.
Level

Easy3 of 10

Topics
Dynamic programming, Simulation
Solved
No attempts yet

Problem

Minjeong is currently at the origin (position 0) of the number line. At any moment, Minjeong can move left or right along the number line at a speed of 1 unit per second. Of course, Minjeong may also stay still.

Minjeong knows that the following event will occur NN times.

  • If Minjeong is strictly between position AA and position BB after TT seconds, Minjeong will be hit by Karina's body bang.

Positions AA and BB are safe.

Minjeong wants to move appropriately so as not to be hit by Karina's body bang. Determine whether Minjeong can avoid the body bang by moving, and if so, minimize the travel distance.

Input

The first line contains the number of events NN.

Each of the next NN lines contains TT, AA, and BB for one event, separated by spaces.

Output

If Minjeong cannot avoid Karina's body bang, print -1.

If Minjeong can avoid Karina's body bang, print the minimum travel distance of Minjeong.

Constraints

  • All given numbers are integers.
  • 1≤N≤1001 \le N \le 100
  • 1≤T≤1 0001 \le T \le 1\,000
  • −1 000≤A<B≤1 000-1\,000 \le A < B \le 1\,000
  • The TT of each event is distinct.

Examples3

  1. Example 1

    Input
    3
    10 -3 7
    20 -8 2
    25 3 9
    
    Expected output
    8
    
  2. Example 2

    Input
    3
    3 -3 7
    7 -8 2
    8 3 9
    
    Expected output
    -1
    
  3. Example 3

    Input
    10
    37 -8 16
    38 -4 16
    12 -42 -31
    24 -9 11
    16 -32 -14
    26 1 18
    11 -43 -40
    18 -17 2
    47 28 35
    33 -50 -49
    
    Expected output
    18