Problem: In the International City of Pipe Construction, it is planned to repair the water pipe at a certain point in the water pipe network. The network consists of water pipe segments, stop valves and source point. A water pipe is represented by a segment on a 2D-plane and intersected pair of water pipe segments are connected at the intersection point. A stop valve, which prevents from water flowing into the repairing point while repairing, is represented by a point on some water pipe segment. In the network, just one source point exists and water is supplied to the network from this point.

Of course, while repairing, we have to stop water supply in some areas, but, in order to reduce the risk of riots, the length of water pipes stopping water supply must be minimized. What you have to do is to write a program to minimize the length of water pipes needed to stop water supply when the coordinates of end points of water pipe segments, stop valves, source point and repairing point are given.



Input

A data set has the following format:

> N M
>  xs1 ys1 xd1 yd1
>  ...
>  xsN ysN xdN ydN
>  xv1 yv1
>  ...
>  xvM yvM
>  xb yb
>  xc yc
>

The first line of the input contains two integers, N (1 ≤ N ≤ 300) and M (0 ≤ M ≤ 1,000) that indicate the number of water pipe segments and stop valves. The following N lines describe the end points of water pipe segments. The i-th line contains four integers, xsi, ysi, xdi and ydi that indicate the pair of coordinates of end points of i-th water pipe segment. The following M lines describe the points of stop valves. The i-th line contains two integers, xvi and yvi that indicate the coordinate of end points of i-th stop valve. The following line contains two integers, xb and yb that indicate the coordinate of the source point. The last line contains two integers, xc and yc that indicate the coordinate of the repairing point.

You may assume that any absolute values of coordinate integers are less than 1,000 (inclusive.) You may also assume each of the stop valves, the source point and the repairing point is always on one of water pipe segments and that that each pair among the stop valves, the source point and the repairing point are different. And, there is not more than one intersection between each pair of water pipe segments. Finally, the water pipe network is connected, that is, all the water pipes are received water supply initially.

Output

Print the minimal length of water pipes needed to stop water supply in a line. The absolute or relative error should be less than or 10-6. When you cannot stop water supply to the repairing point even though you close all stop valves, print "`-1`" in a line.

Examples

Input

1 2
0 0 10 0
1 0
9 0
0 0
5 0


Output

9.0


Input

5 3
0 4 2 4
0 2 2 2
0 0 2 0
0 0 0 4
2 0 2 4
0 2
1 0
2 2
1 4
2 1


Output

3.0


Input

2 1
0 0 0 4
0 2 2 2
1 2
0 1
0 3


Output

-1

Solution (in CPP):

Solution:
#include <iostream>
#include <complex>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <cstdio>
using namespace std;

const double pi  = M_PI;
const double inf = 1e10;
const double eps = 1e-10;
typedef complex<double> point;

struct line : public vector<point> {
	line(const point& a, const point& b) { push_back(a); push_back(b); }
};

namespace std {
	bool operator<(const point& a, const point& b) {
		if(abs(a.real() - b.real()) > eps) return a.real() < b.real();
		if(abs(a.imag() - b.imag()) > eps) return a.imag() < b.imag();
		return false;
	}
}

point  vec  (const line&  l)                 { return l[1]-l[0]; }
double dot  (const point& a, const point& b) { return (a*conj(b)).real(); }
double cross(const point& a, const point& b) { return (conj(a)*b).imag(); }

int ccw(const point& a, const point& b, const point& c)
{
	point u=b-a, v=c-a;
	if(cross(u,v) > 0 ) return +1;
	if(cross(u,v) < 0 ) return -1;
	if(  dot(u,v) < 0 ) return +2;
	if(abs(u) < abs(v)) return -2;
	return 0;
}

int ccw(const line& s, const point& p) {
	return ccw(s[0], s[1], p);
}

bool intersectSP(const line& s, const point& p) {
	return abs(s[0]-p)+abs(s[1]-p) < abs(s[1]-s[0])+eps;
}

bool intersectSS(const line& s, const line& t) {
	return ccw(s,t[0])*ccw(s,t[1]) <= 0
		&& ccw(t,s[0])*ccw(t,s[1]) <= 0;
}

point crosspoint(const line& l, const line& m) {
	double A = cross(vec(l), vec(m));
	double B = cross(vec(l), l[1]-m[0]);
	if(abs(A) < eps) return m[0];
	return m[0] + B/A*vec(m);
}

enum ntype{ NONE, VALVE, SOURCE, REPAIR };

struct node
{
	point pos;
	ntype type;
	vector<int> to;

	node(const point& p, ntype t) : pos(p), type(t) {}
};

vector<node> seg_arr(const vector<line>& lines, const vector<point>& points)
{
	map<point, ntype> p;
	for(int i=0; i<points.size(); i++) {
		ntype type = VALVE;
		if(i == points.size()-2) { type = SOURCE; }
		if(i == points.size()-1) { type = REPAIR; }

		p.insert( make_pair(points[i],type) );
	}

	for(int i=0; i<lines.size(); i++) {
		p.insert( make_pair(lines[i][0],NONE) );
		p.insert( make_pair(lines[i][1],NONE) );
		for(int j=i+1; j<lines.size(); j++) {
			if(intersectSS(lines[i], lines[j])) {
				point cp = crosspoint(lines[i], lines[j]);
				p.insert( make_pair(cp,NONE) );
			}
		}
	}

	vector<node> graph;
	for(map<point,ntype>::iterator it=p.begin(); it!=p.end(); it++) {
		graph.push_back( node(it->first,it->second) );
	}

	for(int i=0; i<lines.size(); i++) {
		vector<pair<double,int> > online;
		for(int j=0; j<graph.size(); j++) {
			if(intersectSP(lines[i], graph[j].pos)) {
				online.push_back( pair<double,int>(abs(lines[i][0]-graph[j].pos), j) );
			}
		}

		sort(online.begin(), online.end());
		for(int j=1; j<online.size(); j++) {
			int a = online[j-1].second;
			int b = online[j  ].second;
			graph[a].to.push_back(b);
			graph[b].to.push_back(a);
		}
	}

	return graph;
}

int main()
{
	int N,M;
	cin >> N >> M;

	vector<line>  lines;
	vector<point> points;

	int x1,y1,x2,y2;
	for(int i=0; i<N; i++) {
		cin >> x1 >> y1 >> x2 >> y2;
		lines.push_back( line(point(x1,y1), point(x2,y2)) );
	}
	for(int i=0; i<M+2; i++) {
		cin >> x1 >> y1;
		points.push_back( point(x1,y1) );
	}

	int source,repair;
	vector<node> graph = seg_arr(lines,points);
	for(int i=0; i<graph.size(); i++) {
		if(graph[i].type == SOURCE) { source = i; }
		if(graph[i].type == REPAIR) { repair = i; }
	}


/*
	printf("%d -> %d\n", source, repair);
	for(int i=0; i<graph.size(); i++) {
		printf("%2i : (%3lf,%3lf)\t%d [ ", i, graph[i].pos.real(), graph[i].pos.imag(), graph[i].type);
		for(int j=0; j<graph[i].to.size(); j++) {
			printf("%2d ", graph[i].to[j]);
		}
		printf("]\n");
	}
*/

	vector<bool> v(graph.size(), 0);

	queue<int> q;
	q.push(repair);
	v[repair] = 1;
	while(!q.empty())
	{
		int t=q.front(); q.pop();

		if(graph[t].type == VALVE) continue;
		if(graph[t].type == SOURCE) {
			cout << -1 << endl;
			return 0;
		}

		for(int i=0; i<graph[t].to.size(); i++) {
			int s = graph[t].to[i];
			if(v[s]) continue;
			v[s] = 1;
			q.push(s);
		}
	}

	double wl = 0;
	set<pair<int,int> > ve;
	q.push(source);
	v[source] = 1;
	while(!q.empty())
	{
		int t=q.front(); q.pop();

		for(int i=0; i<graph[t].to.size(); i++) {
			int s = graph[t].to[i];

			if(ve.insert(make_pair(min(t,s),max(t,s))).second) {
				wl += abs(graph[t].pos - graph[s].pos);
			}

			if(v[s]) continue;
			v[s] = 1;
			q.push(s);
		}
	}


	double al = 0;
	ve.clear();
	for(int i=0; i<graph.size(); i++)
	for(int j=0; j<graph[i].to.size(); j++) {
		int t=i, s=graph[i].to[j];
		if(ve.insert(make_pair(min(t,s),max(t,s))).second) {
			al += abs(graph[t].pos - graph[s].pos);
		}
	}
	printf("%.9lf\n", al-wl);

}