#include<iostream>
using namespace std;
int SX;
int SY;
int TX;
int TY;
// Since the wormhole is biderctional the two entrance points are stored in (WSX, WSY) ans (WDX , WDY)
// WP helps in tracking which entrance point was used. WSX or WSY is represnted using a 0 in WP and WDX and WDY is represented using a 1
int WSX[100];
int WSY[100];
int WDX[100];
int WDY[100];
int WC[100];
int WP[100]; // Helps in tracking which entrance point was used
int N;
int dist(int a, int b)
{
int ele = a - b;
if(ele < 0)
return -ele;
return ele;
}
void greed(int * visitc, bool * choosen)
{
int mini = 99999999;
int node = -1;
for(int i = 0; i <= N; ++i)
{
if(choosen[i] == false && mini > visitc[i])
{
mini = visitc[i];
node = i;
}
}
if(node == N)
return;
choosen[node] = true;
for(int i = 0; i <= N; ++i)
{
if(i == node || choosen[i] == true)
continue;
int temp0;
if(WP[node] == 0)
temp0 += ( dist(WSX[i], WDX[node]) + dist(WSY[i], WDY[node]) );
else
temp0 += ( dist(WSX[i], WSX[node]) + dist(WSY[i], WSY[node]) );
int temp1;
if(WP[node] == 0)
temp1 += ( dist(WDX[i], WDX[node]) + dist(WDY[i], WDY[node]) );
else
temp1 += ( dist(WDX[i], WSX[node]) + dist(WDY[i], WSY[node]) );
int z;
int temp;
if(temp0 < temp1)
{
temp = temp0;
z = 0;
}
else
{
temp = temp1;
z = 1;
}
temp += WC[i];
if(visitc[i] > visitc[node] + temp)
{
visitc[i] = visitc[node] + temp;
WP[i] = z;
}
}
}
int main()
{
cin>>SX>>SY;
cin>>TX>>TY;
cin>>N;
for(int i = 0; i < N; ++i)
{
cin>>WSX[i]>>WSY[i];
cin>>WDX[i]>>WDY[i];
cin>>WC[i];
}
int visitc[N + 1];
bool choosen[N + 1];
int ans = (dist(SX, TX) + dist(SY, TY));
visitc[N] = ans;
for(int i = 0; i < N; ++i)
{
choosen[i] = false;
int o1 = (dist(SX, WSX[i]) + dist(SY, WSY[i]));
int o2 = (dist(SX, WDX[i]) + dist(SY, WDY[i]));
visitc[i] = WC[i];
if(o1 < o2)
{
WP[i] = 0;
visitc[i] += o1;
}
else
{
WP[i] = 1;
visitc[i] += o2;
}
}
greed(visitc, choosen);
cout<<visitc[N]<<endl;
return 0;
}
I2luY2x1ZGU8aW9zdHJlYW0+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IFNYOwppbnQgU1k7CmludCBUWDsKaW50IFRZOwoKCi8vIFNpbmNlIHRoZSB3b3JtaG9sZSBpcyBiaWRlcmN0aW9uYWwgdGhlIHR3byBlbnRyYW5jZSBwb2ludHMgYXJlIHN0b3JlZCBpbiAoV1NYLCBXU1kpIGFucyAoV0RYICwgV0RZKQovLyBXUCBoZWxwcyBpbiB0cmFja2luZyB3aGljaCBlbnRyYW5jZSBwb2ludCB3YXMgdXNlZC4gV1NYIG9yIFdTWSBpcyByZXByZXNudGVkIHVzaW5nIGEgMCBpbiBXUCBhbmQgV0RYIGFuZCBXRFkgaXMgcmVwcmVzZW50ZWQgdXNpbmcgYSAxCgppbnQgV1NYWzEwMF07CmludCBXU1lbMTAwXTsKaW50IFdEWFsxMDBdOwppbnQgV0RZWzEwMF07CmludCBXQ1sxMDBdOwppbnQgV1BbMTAwXTsgLy8gSGVscHMgaW4gdHJhY2tpbmcgd2hpY2ggZW50cmFuY2UgcG9pbnQgd2FzIHVzZWQKaW50IE47CgoKaW50IGRpc3QoaW50IGEsIGludCBiKQp7CiAgICBpbnQgZWxlID0gYSAtIGI7CgogICAgaWYoZWxlIDwgMCkKICAgICAgICByZXR1cm4gLWVsZTsKCiAgICByZXR1cm4gZWxlOwp9CgoKdm9pZCBncmVlZChpbnQgKiB2aXNpdGMsIGJvb2wgKiBjaG9vc2VuKQp7CgogICAgICAgIGludCBtaW5pID0gOTk5OTk5OTk7CiAgICAgICAgaW50IG5vZGUgPSAtMTsKCiAgICAgICBmb3IoaW50IGkgPSAwOyBpIDw9IE47ICsraSkKICAgICAgIHsKICAgICAgICAgICAgaWYoY2hvb3NlbltpXSA9PSBmYWxzZSAmJiBtaW5pID4gdmlzaXRjW2ldKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBtaW5pID0gdmlzaXRjW2ldOwogICAgICAgICAgICAgICAgbm9kZSA9IGk7CiAgICAgICAgICAgIH0KICAgICAgICB9CgoKICAgICAgICBpZihub2RlID09IE4pCiAgICAgICAgICAgIHJldHVybjsKCgogICAgICAgY2hvb3Nlbltub2RlXSA9IHRydWU7CgoKCiAgICAgICAgZm9yKGludCBpID0gMDsgaSA8PSBOOyArK2kpCiAgICAgICAgewogICAgICAgICAgICBpZihpID09IG5vZGUgfHwgY2hvb3NlbltpXSA9PSB0cnVlKQogICAgICAgICAgICAgICAgY29udGludWU7CgogICAgICAgICAgICBpbnQgdGVtcDA7CgogICAgICAgICAgICBpZihXUFtub2RlXSA9PSAwKQogICAgICAgICAgICAgICAgdGVtcDAgKz0gKCBkaXN0KFdTWFtpXSwgV0RYW25vZGVdKSArIGRpc3QoV1NZW2ldLCBXRFlbbm9kZV0pICk7CgogICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICAgdGVtcDAgKz0gKCBkaXN0KFdTWFtpXSwgV1NYW25vZGVdKSArIGRpc3QoV1NZW2ldLCBXU1lbbm9kZV0pICk7CgoKICAgICAgICAgICAgaW50IHRlbXAxOwoKICAgICAgICAgICAgaWYoV1Bbbm9kZV0gPT0gMCkKICAgICAgICAgICAgICAgIHRlbXAxICs9ICggZGlzdChXRFhbaV0sIFdEWFtub2RlXSkgKyBkaXN0KFdEWVtpXSwgV0RZW25vZGVdKSApOwoKICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgIHRlbXAxICs9ICggZGlzdChXRFhbaV0sIFdTWFtub2RlXSkgKyBkaXN0KFdEWVtpXSwgV1NZW25vZGVdKSApOwoKICAgICAgICAgICAgaW50IHo7CiAgICAgICAgICAgIGludCB0ZW1wOwoKICAgICAgICAgICAgIGlmKHRlbXAwIDwgdGVtcDEpCiAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgdGVtcCA9IHRlbXAwOwogICAgICAgICAgICAgICAgIHogPSAwOwogICAgICAgICAgICAgfQoKICAgICAgICAgICAgIGVsc2UKICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICB0ZW1wID0gdGVtcDE7CiAgICAgICAgICAgICAgICAgeiA9IDE7CiAgICAgICAgICAgICB9CgogICAgICAgICAgICAgdGVtcCArPSBXQ1tpXTsKCiAgICAgICAgICAgIGlmKHZpc2l0Y1tpXSA+IHZpc2l0Y1tub2RlXSArIHRlbXApCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIHZpc2l0Y1tpXSA9IHZpc2l0Y1tub2RlXSArIHRlbXA7CiAgICAgICAgICAgICAgICBXUFtpXSA9IHo7CiAgICAgICAgICAgIH0KCiAgICAgICAgfQp9CgoKaW50IG1haW4oKQp7CgogICAgY2luPj5TWD4+U1k7CiAgICBjaW4+PlRYPj5UWTsKCiAgICBjaW4+Pk47CgogICAgZm9yKGludCBpID0gMDsgaSA8IE47ICsraSkKICAgIHsKCiAgICAgICAgY2luPj5XU1hbaV0+PldTWVtpXTsKICAgICAgICBjaW4+PldEWFtpXT4+V0RZW2ldOwogICAgICAgIGNpbj4+V0NbaV07CiAgICB9CgoKICAgIGludCB2aXNpdGNbTiArIDFdOwogICAgYm9vbCBjaG9vc2VuW04gKyAxXTsKCiAgICBpbnQgYW5zID0gKGRpc3QoU1gsIFRYKSArIGRpc3QoU1ksIFRZKSk7CgogICAgdmlzaXRjW05dID0gYW5zOwoKICAgIGZvcihpbnQgaSA9IDA7IGkgPCBOOyArK2kpCiAgICB7CiAgICAgICAgY2hvb3NlbltpXSA9IGZhbHNlOwoKICAgICAgICBpbnQgbzEgPSAoZGlzdChTWCwgV1NYW2ldKSArIGRpc3QoU1ksIFdTWVtpXSkpOwogICAgICAgIGludCBvMiA9IChkaXN0KFNYLCBXRFhbaV0pICsgZGlzdChTWSwgV0RZW2ldKSk7CgogICAgICAgIHZpc2l0Y1tpXSA9IFdDW2ldOwoKICAgICAgICBpZihvMSA8IG8yKQogICAgICAgIHsKICAgICAgICAgICAgV1BbaV0gPSAwOwogICAgICAgICAgICB2aXNpdGNbaV0gKz0gbzE7CiAgICAgICAgfQoKICAgICAgIGVsc2UKICAgICAgIHsKICAgICAgICAgICAgIFdQW2ldID0gMTsKICAgICAgICAgICAgIHZpc2l0Y1tpXSArPSBvMjsKICAgICAgIH0KCiAgICB9CgoKCiAgICBncmVlZCh2aXNpdGMsIGNob29zZW4pOwoKICAgIGNvdXQ8PHZpc2l0Y1tOXTw8ZW5kbDsKCiAgICByZXR1cm4gMDsKfQo=