#include using namespace std; using ll = long long; using ld = long double; // #define int long long #define pii pair #define pll pair #define vi vector #define all(v) (v).begin(), (v).end() #define rall(v) (v).rbegin(), (v).rend() #define mp make_pair #define pb push_back #define sz(v) (int)(v).size() // #define endl '\n' const ld eps = 1e-8; const ll mod = 1e9 + 7; const int inf0 = 1e9 + 10; const ll inf = 2e18 + 10; void solve(); signed main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); #ifdef LC freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); #endif cout << fixed << setprecision(20); int t = 1; //cin >> t; while (t--) { solve(); } return 0; } struct Vect { ld x, y; Vect() {} Vect(ld xx, ld yy): x(xx), y(yy) {} }; Vect operator+(const Vect &a, const Vect &b) { return Vect(a.x + b.x, a.y + b.y); } Vect operator-(const Vect &a, const Vect &b) { return Vect(a.x - b.x, a.y - b.y); } Vect operator*(const Vect &a, ld k) { return Vect(a.x * k, a.y * k); } Vect operator/(const Vect &a, ld k) { return Vect(a.x / k, a.y / k); } ld mul(const Vect &a, const Vect &b) { return a.x * b.x + a.y * b.y; } ld cross(const Vect &a, const Vect &b) { return a.x * b.y - a.y * b.x; } ld len(const Vect &a) { return sqrtl(mul(a, a)); } Vect ein(const Vect &a) { ld lenn = len(a); if (lenn < eps) { return Vect(0, 0); } return Vect(a.x / lenn, a.y / lenn); } const int N = 1e6; Vect p[N + 1]; ld dist[N]; ld dist_to_seg(Vect p, Vect a, Vect b) { if (mul(p - a, b - a) >= 0 && mul(p - b, a - b) >= 0) { return abs(cross(p - a, b - a)) / len(b - a); } return min(len(a - p), len(b - p)); } void solve() { ld delta; cin >> delta; int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> p[i].x >> p[i].y; } p[n] = p[n - 1]; dist[0] = 0; for (int i = 1; i < n; ++i) { dist[i] = dist[i - 1] + len(p[i] - p[i - 1]); } ld ans = delta; for (int a = 0; a < n - 1; ++a) { if (dist[a] + delta > dist[n - 1] + eps) { break; } int b_l = 0; int b_r = n - 1; while (b_r - b_l > 1) { int b_m = (b_l + b_r) / 2; if (dist[a] + delta >= dist[b_m]) { b_l = b_m; } else { b_r = b_m; } } int b = b_l; Vect start_b = p[b] + ein(p[b + 1] - p[b]) * (dist[a] + delta - dist[b]); Vect start_a = p[a]; Vect finish_a, finish_b; if (dist[a + 1] + delta <= dist[b]) { finish_a = p[a + 1]; finish_b = p[b] + ein(p[b + 1] - p[b]) * len(finish_a - start_a); } else { finish_b = p[b + 1]; finish_a = p[a] + ein(p[a + 1] - p[a]) * len(finish_b - start_b); } ans = min(ans, dist_to_seg(start_a, start_b, finish_b + (start_a - finish_a))); } for (int a = 0; a < n - 1; ++a) { if (dist[a] < delta) { continue; } int b_l = 0; int b_r = n - 1; while (b_r - b_l > 1) { int b_m = (b_l + b_r) / 2; if (dist[a] - delta > dist[b_m]) { b_l = b_m; } else { b_r = b_m; } } int b = b_l; Vect start_b = p[b] + ein(p[b + 1] - p[b]) * (dist[a] - delta - dist[b]); Vect start_a = p[a]; Vect finish_a, finish_b; if (dist[a + 1] - delta <= dist[b]) { finish_a = p[a + 1]; finish_b = p[b] + ein(p[b + 1] - p[b]) * len(finish_a - start_a); } else { finish_b = p[b + 1]; finish_a = p[a] + ein(p[a + 1] - p[a]) * len(finish_b - start_b); } ans = min(ans, dist_to_seg(start_a, start_b, finish_b + (start_a - finish_a))); } cout << ans << '\n'; } // g++ -DLC -o a a.cpp && ./a