<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

146. The Runner
time limit per test: 0.25 sec.
memory limit per test: 4096 KB
input: standard input
output: standard output



The runner moves along the ring road with length L. His way consists of N intervals. First he ran T1 minutes with speed V1, then T2 minutes with speed V2 and so on till the N-th interval, where he ran TN minutes with speed VN. Your task is to find the distance from start to finish along the ring road. The distance along the ring road is the length of the shortest way all points of which belongs to the ring road.

Input
Real number L (1<=L<=1000, with 4 signs after decimal point) and natural number N (N<=20000) are written in the first line. Each of the following N lines contains two integer numbers Ti and Vi (1<=Ti<=10^7, 1<=Vi<=10^6).

Output
Write the only one real number with 4 digits after decimal points: the distance from start to finish.

Sample test(s)

Input
2 1
1 3

Output
1.0000
Author:	Michael R. Mirzayanov
Resource:	Summer School Team Contest, 2002
Date:	August, 2002

<|response|>
1. Abridged Problem Statement  
A runner goes around a circular track of circumference L. He runs N successive intervals, where in interval i he runs for Ti minutes at speed Vi. Compute the shortest distance along the track between his starting point and his finishing point, and print it with four decimal places.

2. Key Observations  
- On a circle, only the runner’s net displacement modulo the circumference matters.  
- After accumulating total distance D, the position on the track is D mod L.  
- The shortest path between start and end is min(s, L − s), where s = (D mod L).  
- Working with real numbers and large products (Ti up to 10^7, Vi up to 10^6, N up to 2×10^4) can cause floating‐point drift or overflow.  
- To avoid precision issues, scale L by 10 000 (the number of decimal places required), work in integers, and only convert back at the end.

3. Full Solution Approach  
a. Define a scaling factor B = 10 000.  
b. Read L as a double, compute Ls = round(L × B) to get an integer circumference.  
c. Initialize an integer accumulator s = 0.  
d. For each of the N intervals:  
   - Read Ti and Vi (both integers).  
   - Compute the scaled distance for this interval: Di = (Ti × Vi × B) mod Ls.  
   - Update s = (s + Di) mod Ls.  
e. After all intervals, s is the runner’s forward displacement (scaled). The shorter arc is best = min(s, Ls−s).  
f. Convert back to real distance: best_real = best / B, and print with four decimal places.

Time complexity is O(N). Space is O(1) extra beyond input.

4. C++ Implementation with Detailed Comments  
```cpp
#include <bits/stdc++.h>
using namespace std;

// Scaling factor for four decimal places
static const int64_t B = 10000;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    // 1. Read and scale the circumference
    double L_in;
    cin >> L_in;
    // Multiply by B and round to nearest integer
    int64_t Ls = int64_t(L_in * B + 0.5);

    // 2. Read number of intervals
    int N;
    cin >> N;

    // 3. Accumulate total displacement modulo Ls
    int64_t s = 0;
    for (int i = 0; i < N; i++) {
        int64_t T, V;
        cin >> T >> V;
        // Compute interval distance scaled by B, reduce mod Ls
        // We do (T*V) first (fits in 64‐bit), then multiply by B, then take mod
        int64_t d = (T * V) % Ls;      // reduce before scaling
        d = (d * B) % Ls;              // scale and reduce
        s = (s + d) % Ls;              // accumulate
    }

    // 4. Determine the shortest way around the circle
    int64_t best = min(s, Ls - s);

    // 5. Convert back to floating‐point and print with 4 decimal places
    cout << fixed << setprecision(4) << (best / double(B)) << "\n";

    return 0;
}
```

5. Python Implementation with Detailed Comments  
```python
import sys

def main():
    data = sys.stdin.read().split()
    # Scaling factor for four decimal places
    B = 10000

    # 1. Read L and scale to integer
    Lf = float(data[0])
    Ls = int(Lf * B + 0.5)

    # 2. Number of intervals
    N = int(data[1])
    idx = 2

    # 3. Accumulate displacement modulo Ls
    s = 0
    for _ in range(N):
        T = int(data[idx]); V = int(data[idx+1])
        idx += 2
        # Compute scaled distance for this interval
        # (T * V) can be large, but Python handles big ints
        d = (T * V) % Ls     # reduce before scaling
        d = (d * B) % Ls     # scale and reduce
        s = (s + d) % Ls

    # 4. Shortest arc: either forward s or backward Ls - s
    best = min(s, Ls - s)

    # 5. Convert back to float and print with four decimals
    print(f"{best / B:.4f}")

if __name__ == "__main__":
    main()
```