#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) x.begin(),x.end()
const long double PI = acos(-1.0L);
const long long MOD = 1000000007LL;
//const long long MOD = 998244353LL;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
template<class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true;} return false; }
template<class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true;} return false; }
///////////////////////////////////////////////////////////////////////////////////////////////////

using DD = double; // 精度が足りないときはlong double
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)

//点
struct Point {
    DD x, y;
    Point (DD x = 0, DD y = 0): x(x), y(y) {}

    Point& operator+=(const Point &p) { x += p.x; y += p.y; return *this;}
    Point operator+(const Point &p) const { return Point(*this) += p;}
    Point& operator-=(const Point &p) { x -= p.x; y -= p.y; return *this;}
    Point operator-(const Point &p) const { return Point(*this) -= p;}
    Point& operator*=(DD a) { x *= a; y *= a; return *this;}
    Point operator*(DD a) const { return Point(*this) *= a;}
    Point& operator/=(DD a) { x /= a; y /= a; return *this;}
    Point operator/(DD a) const { return Point(*this) /= a;}
    bool operator== (const Point &p) const { return fabs(x - p.x) < EPS && fabs(y - p.y) < EPS;}
    friend istream& operator >>(istream& is, Point& p) { is >> p.x >> p.y; return is;}
    friend ostream& operator <<(ostream& os, const Point& p) { os<<p.x<<" "<<p.y; return os;}
};
typedef Point Vector; //ベクトル

inline DD norm(const Vector &a) { return a.x * a.x + a.y * a.y;}
inline DD abs(const Vector &a) { return sqrt(norm(a));}
inline DD dot(const Vector &a, const Vector &b) { return a.x * b.x + a.y * b.y;}
inline DD cross(const Vector &a, const Vector &b) { return a.x * b.y - a.y * b.x;}
inline Point rot(const Point &p, DD ang) { return Point(cos(ang) * p.x - sin(ang) * p.y, sin(ang) * p.x + cos(ang) * p.y);}
inline Point rot90(const Point &p) { return Point(-p.y, p.x);}

//線分
struct Segment {
    Point p1,p2;
    Segment() = default;
    Segment(Point p1, Point p2) : p1(p1),p2(p2) {}
};

typedef Segment Line;
/*
//直線
struct Line : Segment {
    Line() = default;
    Line(Point p1, Point p2) : Segment(p1, p2) {}
};
*/
struct Circle {
    Point c;
    DD r;
    Circle(Point c = Point(), DD r = 0): c(c), r(r) {}
};

typedef vector<Point> Polygon; //多角形

//射影
Point project(const Segment &s, const Point &p) {
    Vector base = s.p2 - s.p1;
    DD r = dot(p - s.p1, base) / norm(base);
    return s.p1 + base * r;
}
inline Point reflect(const Segment &s, const Point &p) {return p + (project(s,p) - p) * (DD)2;} //線対称

int ccw(const Point &p0, const Point &p1, const Point &p2) {
    Vector a = p1 - p0;
    Vector b = p2 - p0;
    if (cross(a,b) > EPS) return 1; //p0,p1から見てp2は左側(反時計回り)
    if (cross(a,b) < -EPS) return -1; //p0,p1から見てp2は右側(時計回り)
    if (dot(a,b) < -EPS) return 2; //p2-p0-p1の順に一直線上
    if (norm(a) < norm(b)) return -2; //p0-p1-p2の順に一直線上
    return 0; //p0-p2-p1の順に一直線上
}
//線分p1,p2とp3,p4の交差判定
inline bool intersect(const Point &p1, const Point &p2, const Point &p3, const Point &p4) { return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 && ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0);}
inline bool intersect(const Segment &s1, const Segment &s2) { return intersect(s1.p1, s1.p2, s2.p1, s2.p2);}

inline DD getDistanceLP(const Line &l, const Point &p) { return abs(cross(l.p2 - l.p1,p - l.p1) / abs(l.p2 - l.p1));}
DD getDistanceSP(const Segment &s, const Point &p) {
    if (dot(s.p2 - s.p1,p - s.p1) < 0.0) return abs(p - s.p1);
    if (dot(s.p1 - s.p2,p - s.p2) < 0.0) return abs(p - s.p2);
    return getDistanceLP(s, p);
}
DD getDistance(const Segment &s1, const Segment &s2) {
    if (intersect(s1, s2)) return (DD)0;
    return min({getDistanceSP(s1, s2.p1), getDistanceSP(s1, s2.p2), getDistanceSP(s2, s1.p1), getDistanceSP(s2, s1.p2)});
}

//線分と線分の交点
Point getCrossPoint(const Segment &s1, const Segment &s2) {
    Vector base = s2.p2 - s2.p1;
    DD d1 = abs(cross(base, s1.p1 - s2.p1));
    DD d2 = abs(cross(base, s1.p2 - s2.p1));
    DD t = d1 / (d1 + d2);
    return s1.p1 + (s1.p2 - s1.p1) * t;
}

//円と直線の交差判定
inline bool intersect(const Circle &c, const Line &l) { return getDistanceLP(l, c.c) - c.r < EPS;}
//円と円の交差判定
inline bool intersect(const Circle &c1, const Circle &c2) { return abs(c1.c - c2.c) - (c1.r + c2.r) < EPS && fabs(c1.r - c2.r) - abs(c1.c - c2.c) < EPS;}

//円と線分の交点
pair<Point, Point> getCrossPoints(const Circle &c, const Line &l) {
    assert(intersect(c,l));
    Vector pr = project(l, c.c);
    Vector e = (l.p2 - l.p1) / abs(l.p2 - l.p1);
    DD base = sqrt(c.r * c.r - norm(pr - c.c));
    return make_pair(pr + e * base, pr - e * base);
}

inline DD arg(const Vector &p) { return atan2(p.y, p.x);}
inline Vector polar(DD a, DD r) { return Point(cos(r) * a, sin(r) * a);}

//円と円の交点
pair<Point, Point> getCrossPoints(const Circle &c1, const Circle &c2) {
    assert(intersect(c1, c2));
    DD d = abs(c1.c - c2.c);
    DD a = acos((c1.r * c1.r + d * d - c2.r * c2.r) / (DD(2) * c1.r * d));
    DD t = arg(c2.c - c1.c);
    return make_pair(c1.c + polar(c1.r, t + a), c1.c + polar(c1.r, t - a));
}
int ort(const Point &a) { //象限
    if (norm(a) < EPS) return 0;
    if (a.y > 0) return a.x > 0 ? 1 : 2;
    return a.x > 0 ? 4 : 3;
}

bool xsort(const Point &a, const Point &b) {
    if (equals(a.x, b.x)) return a.y < b.y;
    return a.x < b.x;
}

bool ysort(const Point &a, const Point &b) {
    if (equals(a.y, b.y)) return a.x < b.x;
    return a.y < b.y;
}

bool argsortcross(const Point &a, const Point &b) {
    int ao = ort(a), bo = ort(b);
    if (ao != bo) return ao < bo;
    return cross(a,b) > 0;
}

bool argsortatan2(const Point &a, const Point &b) {
    return atan2(a.y, a.x) < atan2(b.y, b.x);
}

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    Point s,t; cin >> s >> t;
    int N; cin >> N;
    vector<Circle> A(N+2);
    A[0] = s; A[N+1] = t;
    rep(i,N) {
        Point x; DD r; cin >> x >> r;
        A[i+1] = {x,r};
    }

    vector<vector<DD>> G(N+2, vector<DD>(N+2));
    for (int i = 0; i < N+2; i++) {
        for (int j = i+1; j < N+2; j++) {
            G[i][j] = G[j][i] = max(0.0,abs(A[i].c-A[j].c) - A[i].r - A[j].r);
        }
    }

    vector<DD> dist(N+2,1e18);
    vector<bool> check(N+2,false);
    dist[0] = 0;
    check[0] = true;
    int v = 0;
    while (1) {
        DD d = 1e18;
        int u = -1;
        rep(nv,N+2) {
            if (check[nv]) continue;
            chmin(dist[nv],dist[v]+G[v][nv]);
            if (chmin(d,dist[nv])) u = nv;             
        }
        if (u == -1) break;
        v = u;
        check[v] = true;
    }

    cout << fixed << setprecision(10) << dist[N+1] << endl;
}

Measured memory_bytes: 8192