The Musi River runs through the city of Palembang and splits it into two districts. Call them district A and district B.
Each district has exactly 1,000,000,001 buildings along the bank, numbered 0 to 1,000,000,000 in order. Two neighbouring buildings are 1 unit apart, and the river is 1 unit wide. Building i of district A sits directly across the river from building i of district B.
N citizens live and work in the city. Citizen i lives in building Si of district Pi and works in building Ti of district Qi. A citizen whose home and office are in different districts has had to cross the river by boat. Boats are inconvenient, so the city wants to build at most K bridges so that every citizen can commute by car alone. A bridge must be perpendicular to the river, so it joins two buildings with the same number, and two different bridges use different numbers.
After the bridges are built, let Di be the shortest distance citizen i can drive from home to the office. Place the bridges so that D1+D2+⋯+DN is as small as possible, and report that minimum.
The first line contains K and N. Each of the next N lines contains Pi, Si, Qi, Ti, separated by spaces.
Print the minimum total commuting distance on one line.
This picture shows both example inputs.

One optimal placement for the first example. The pink part is the bridge.

One optimal placement for the second example.
