Problem: Dr. Asimov, a robotics researcher, has succeeded in developing a new robot. The robot can move freely along a special wire stretched around the floor. The robot always moves forward in a direction parallel to the current wire.

The good news is that this robot doesn't consume energy to move on the wire, regardless of its distance. However, in order to change the direction at the end point of the wire or the intersection of the wires, it is necessary to rotate around that point, and energy is consumed by the rotation angle.

I want to move this robot from the start point to the goal point. For example, in the figure below, place the robot at start in the east direction (the positive direction of the x-axis in the figure below) and move it to the goal. There is a route from start to the goal via intersection A and intersection B, and a route via intersection A and intersection C, but the former has a longer travel distance but a smaller rotation angle, so it consumes less energy.


<image>


Your job is to create a program that reads the position of the wires on the floor, the start point, the goal point, and reports the minimum total rotation angle required to move from the start point to the goal point.

The robot may be placed in any direction at the start point and in any direction at the goal point.



Input

The input consists of multiple datasets. The format of each dataset is as follows:


n
x11 y11 x21 y21
x12 y12 x22 y22
..
..
..
x1n y1n x2n y2n
sx sy gx gy


n is an integer indicating the number of wires. x1i y1i shows the coordinates of one end point of the i-th wire, and x2i y2i shows the coordinates of the other end point of the i-th wire.

sx sy and gx gy indicate the coordinates of the start point and the goal point, respectively.

All given coordinates are integer values, and the x-axis and y-axis values ​​are -1000 or more and 1000 or less. It may also be assumed that the start and finish points are on the end points of the given wire.

You can assume that n ≤ 50.

When n is 0, it indicates the end of input.

Output

For each dataset, output the minimum value of the total rotation angle on one line. If the robot cannot reach the goal point, output "-1". The output may contain an error of 0.00001 or less.

Example

Input

8
1 3 11 3
5 3 13 11
10 10 17 10
11 2 11 6
10 5 14 5
13 6 13 2
17 10 17 3
13 3 19 3
1 3 19 3
6
1 3 11 3
5 3 13 11
10 10 17 10
11 2 11 6
10 5 14 5
13 3 19 3
1 3 19 3
2
0 0 7 0
3 0 7 3
0 0 7 3
0


Output

270.0000
-1
36.86989765

Solution (in CPP):

Solution:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <tuple>
#include <vector>
using namespace std;

constexpr double EPS = 1e-9;

struct point {
	double x, y;
	explicit point(double x_ = 0.0, double y_ = 0.0):x(x_), y(y_) {}
	point(const point &p):x(p.x), y(p.y) {}

	point operator+ (const point &p) const {
		return point(x + p.x, y + p.y);
	}

	point operator- (const point &p) const {
		return point(x - p.x, y - p.y);
	}

	point operator* (double s) const {
		return point(x * s, y * s);
	}

	point operator* (const point &p) const {
		return point(x * p.x - y * p.y, x * p.y + y * p.x);
	}

	point operator/ (double s) const {
		return point(x / s, y / s);
	}

	bool operator< (const point &p) const {
		return x + EPS < p.x || (abs(x - p.x) < EPS && y + EPS < p.y);
	}

	bool operator== (const point &p) const {
		return abs(x - p.x) < EPS && abs(y - p.y) < EPS;
	}
};

double angle(const point &p) {
	return atan2(p.y, p.x);
}

double norm(const point &p) {
	return p.x * p.x + p.y * p.y;
}

double abs(const point &p) {
	return sqrt(norm(p));
}

double dot(const point &a, const point &b) {
	return a.x * b.x + a.y * b.y;
}

double cross(const point &a, const point &b) {
	return a.x * b.y - a.y * b.x;
}

struct segment {
	point a, b;
	segment(const point &a_, const point &b_):a(a_), b(b_){}
};

int ccw(const point &a, point b, point c) {
	b = b - a;
	c = c - a;
	const double tmp = cross(b, c);
	if(tmp > EPS) return 1; // ccw
	if(tmp < -EPS) return -1; // cw
	if(dot(b, c) < 0) return 2; // c, a, b 順に一直線上
	if(norm(b) < norm(c)) return -2; // a, b, c 順に一直線上
	return 0; //a, c, b 順で一直線上
}

bool intersect(const segment &s, const segment &t) {
	return ccw(s.a, s.b, t.a) * ccw(s.a, s.b, t.b) <= 0
		&& ccw(t.a, t.b, s.a) * ccw(t.a, t.b, s.b) <= 0;
}

bool intersect(const segment &s, const point &p) {
	return ccw(s.a, s.b, p) == 0;
}

// 先にintersectで交差判定をすること
point crosspoint(const segment &s, const segment &t) {
	const double tmp = cross(s.b - s.a, t.b - t.a);
	if(abs(tmp) < EPS) { // 一直線上
		if(intersect(s, t.a)) return t.a;
		if(intersect(s, t.b)) return t.b;
		if(intersect(t, s.a)) return s.a;
		return s.b;
	}
	return t.a + (t.b - t.a) * (cross(s.b - s.a, s.b - t.a) / tmp);
}

struct edge {
	int to;
	double cost, theta;
	int rev;
	edge(int to_, double cost_, double theta_, int rev_):to(to_), cost(cost_), theta(theta_), rev(rev_) {}
};

typedef vector<vector<edge>> graph;

graph arrangement(const vector<segment> &segments, vector<point> &points) {
	const int n = segments.size();
	points.clear();

	for(int i = 0; i < n; ++i) {
		const auto &s1 = segments[i];
		points.emplace_back(s1.a);
		points.emplace_back(s1.b);

		for(int j = i + 1; j < n; ++j) {
			const auto &s2 = segments[j];
			if(intersect(s1, s2)) {
				points.emplace_back(crosspoint(s1, s2));
			}
		}
	}

	sort(points.begin(), points.end());
	points.erase(unique(points.begin(), points.end()), points.end());

	const int V = points.size();
	graph G(V);

	for(const auto &s : segments) {
		vector<pair<double, int>> vs;
		for(int i = 0; i < V; ++i) {
			if(intersect(s, points[i])) {
				vs.emplace_back(abs(s.a - points[i]), i);
			}
		}

		sort(vs.begin(), vs.end());
		for(int i = 1; i < static_cast<int>(vs.size()); ++i) {
			const int v = vs[i].second;
			const int u = vs[i - 1].second;
			const double d = vs[i].first - vs[i - 1].first;
			G[v].emplace_back(u, d, angle(points[u] - points[v]) * 180.0 / M_PI, G[u].size());
			G[u].emplace_back(v, d, angle(points[v] - points[u]) * 180.0 / M_PI, G[v].size() - 1);
		}
	}

	return G;
}

typedef tuple<double, int, int> state; // dist, v, from edge

double dijkstra(int s, int g, const graph &G) {
	const int n = G.size();
	if(s >= n || g >= n) return -1;
	if(s == g) return 0;


	vector<map<int, double>> dist(n);
	priority_queue<state, vector<state>, greater<state>> que;

	for(const auto &e : G[s]) {
		dist[e.to][e.rev] = 0.0;
		que.push(state(0.0, e.to, e.rev));
	}

	while(!que.empty()) {
		int v, from;
		double d;
		tie(d, v, from) = que.top();
		que.pop();

		if(dist[v][from] + EPS < d) continue;
		if(v == g) return d;

		const double current_angle = G[v][from].theta - 180.0;

		for(const auto &e : G[v]) {
			double diff_angle = e.theta - current_angle;
			if(diff_angle > 180.0) diff_angle -= 360.0;

			const double next_dist = d + abs(diff_angle);
			if(!dist[e.to].count(e.rev) || dist[e.to][e.rev] > next_dist + EPS) {
				dist[e.to][e.rev] = next_dist;
				que.push(state(next_dist, e.to, e.rev));
			}
		}
	}
	

	return -1;
}

int main() {
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout.setf(ios::fixed);
	cout.precision(10);

	for(int n; cin >> n && n;) {
		vector<segment> segments;
		segments.reserve(n);

		for(int i = 0; i < n; ++i) {
			int x1, y1, x2, y2;
			cin >> x1 >> y1 >> x2 >> y2;
			segments.emplace_back(point(x1, y1), point(x2, y2));
		}

		vector<point> points;
		const graph G = arrangement(segments, points);

		int sx, sy, gx, gy;
		cin >> sx >> sy >> gx >> gy;

		const int s = lower_bound(points.begin(), points.end(), point(sx, sy)) - points.begin();
		const int g = lower_bound(points.begin(), points.end(), point(gx, gy)) - points.begin();

		const double ans = dijkstra(s, g, G);
		if(ans < 0) {
			cout << "-1" << endl;
		}
		else {
			cout << ans << endl;
		}
	}

	return EXIT_SUCCESS;
}