This page is still under construction.

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

Avoiding Airports

Time limit3sMemory limit512 MB

Summary
Find a flight itinerary from country 1 to country n minimizing the sum of squared waiting times at airports.
Level

Hard8 of 10

Topics
Graph, Shortest path, Dynamic programming, Sorting
Solved
No attempts yet

Problem

David is planning a trip around the world. He can visit nn countries, and mm flights are available to him. Flight ii leaves country aia_i at time sis_i and lands in country bib_i at time eie_i.

David is in the airport of country 11 at time 00, and he wants to reach country nn. The total time the trip takes does not bother him, but he hates waiting in an airport. Waiting tt units of time in an airport gives him t2t^2 units of frustration. The time he spends in the airport of country 11 from time 00 until his first flight departs counts as waiting too.

Find an itinerary that minimizes the total frustration.

Input

The first line contains two space separated integers nn and mm. (2≤n≤200 0002 \le n \le 200\,000, 1≤m≤200 0001 \le m \le 200\,000)

Each of the next mm lines contains four space separated integers aia_i, bib_i, sis_i, and eie_i. (1≤ai,bi≤n1 \le a_i, b_i \le n, 0≤si≤ei≤1060 \le s_i \le e_i \le 10^6) This describes a flight from country aia_i to country bib_i that departs at time sis_i and lands at time eie_i.

A flight might have the same departure and arrival country.

No two flights have the same departure time, and no two flights have the same arrival time. No flight has the same arrival time as the departure time of another flight. There is always a way for David to reach country nn.

Output

Print the minimum total frustration on a single line.

Hint

In the first example the cheapest itinerary is this one.

  • The fifth flight of the input. It goes from country 11 to country 22, departing at time 33 and landing at time 88.
  • The third flight. It goes from country 22 to country 11, departing at time 99 and landing at time 1212.
  • The seventh flight. It goes from country 11 to country 33, departing at time 1313 and landing at time 2727.
  • The eighth flight. It goes from country 33 to country 55, departing at time 2828 and landing at time 100100.

The four waits cost 323^2, 121^2, 121^2, and 121^2, so the total frustration is 1212. Another itinerary gets David to his destination sooner, but its total frustration is higher.

Examples2

  1. Example 1

    Input
    5 8
    1 2 1 10
    2 4 11 16
    2 1 9 12
    3 5 28 100
    1 2 3 8
    4 3 20 21
    1 3 13 27
    3 5 23 24
    
    Expected output
    12
    
  2. Example 2

    Input
    3 5
    1 1 10 20
    1 2 30 40
    1 2 50 60
    1 2 70 80
    2 3 90 95
    
    Expected output
    1900