<|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).
   - Add the scaled distance for this interval, Ti × Vi × B, into s.
   - Reduce s modulo Ls. Since Ti × Vi fits in 64 bits and B is small, the product Ti × Vi × B is computed with 64-bit arithmetic before taking the modulus.
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(N) for the stored intervals (or O(1) extra if processed on the fly).

4. C++ Implementation
```cpp
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream& operator<<(ostream& out, const pair<T1, T2>& x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream& operator>>(istream& in, pair<T1, T2>& x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream& operator>>(istream& in, vector<T>& a) {
    for(auto& x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

const int64_t B = 10000;

int64_t L;
int N;
vector<pair<int64_t, int64_t>> a;

void read() {
    double _L;
    cin >> _L;
    L = (int64_t)(_L * B + 0.5);
    cin >> N;
    a.resize(N);
    cin >> a;
}

void solve() {
    // The total path length is the sum of T_i * V_i over the intervals; the
    // finishing position on the ring is that sum modulo L, and the answer is
    // the shorter arc, min(s, L - s). To stay in exact integers we scale all
    // lengths by B = 10000 (L has 4 decimals, T and V are integers), so every
    // distance is an integer number of ten-thousandths. We accumulate
    // T_i * V_i * B modulo L and finally divide back by B for output.

    int64_t s = 0;
    for(int i = 0; i < N; i++) {
        s += a[i].first * 1ll * a[i].second * B;
        s %= L;
    }

    s = min(s, L - s);
    cout << setprecision(4) << fixed << (s / (double)B) << endl;
}

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

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    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
        # Add the scaled distance for this interval, reducing mod Ls
        s = (s + T * V * B) % 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()
```
