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 <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

const double EPS = 1.0e-10;
const double PI = acos(-1.0);

class Point
{
public:
    double y, x;
    Point(){
        y = x = 0.0;
    }
    Point(double y0, double x0){
        y = y0;
        x = x0;
    }
    Point operator+(const Point& p) const{
        return Point(y + p.y, x + p.x);
    }
    Point operator-(const Point& p) const{
        return Point(y - p.y, x - p.x);
    }
    Point operator*(double a) const{
        return Point(y * a, x * a);
    }
    Point operator/(double a) const{
        return Point(y / a, x / a);
    }
    double length() const{
        return sqrt(y * y + x * x);
    }
    double dist(const Point& p) const{
        return sqrt(pow(y - p.y, 2) + pow(x - p.x, 2));
    }
    double dot(const Point& p) const{
        return y * p.y + x * p.x; // |a|*|b|*cosθ
    }
    double cross(const Point& p) const{
        return x * p.y - y * p.x; // |a|*|b|*sinθ
    }
};

double segmentPointDist(const Point& a1, const Point& a2, const Point& p)
{
    if((a2-a1).dot(p-a1) < 0)
        return a1.dist(p);
    else if((a1-a2).dot(p-a2) < 0)
        return a2.dist(p);
    else
        return abs((a2-a1).cross(p-a1)) / a1.dist(a2);
}

bool segmentsIntersection(const Point& a1, const Point& a2, const Point& b1, const Point& b2, Point& intersection)
{
    double d = (a2 - a1).cross(b2 - b1);
    if(abs(d) <= EPS){
        if(segmentPointDist(a1, a2, b1) <= EPS){
            intersection = b1;
            return true;
        }
        if(segmentPointDist(a1, a2, b2) <= EPS){
            intersection = b2;
            return true;
        }
        return false;
    }

    double r = (b1 - a1).cross(b2 - b1) / d;
    double s = (b1 - a1).cross(a2 - a1) / d;
    if(r < -EPS || r > 1.0 + EPS || s < -EPS || s > 1.0 + EPS)
        return false;

    intersection = a1 * (1.0 - r) + a2 * r;
    return true;
}

int main()
{
    for(;;){
        int n; // 線分の数
        cin >> n;
        if(n == 0)
            return 0;

        vector<Point> p1(n), p2(n); // 線分の端点
        for(int i=0; i<n; ++i)
            cin >> p1[i].x >> p1[i].y >> p2[i].x >> p2[i].y;

        Point s, g;
        cin >> s.x >> s.y >> g.x >> g.y;

        vector<Point> p; // 線分の交点
        p.push_back(s);
        p.push_back(g);
        for(int i=0; i<n; ++i){
            for(int j=0; j<i; ++j){
                Point intersection;
                if(segmentsIntersection(p1[i], p2[i], p1[j], p2[j], intersection)){
                    bool same = false;
                    for(unsigned k=0; k<p.size(); ++k){
                        if(p[k].dist(intersection) < EPS)
                            same = true;
                    }
                    if(!same)
                        p.push_back(intersection);
                }
            }
        }

        int m = p.size(); // 線分の交点の数
        vector<vector<int> > edges(m);
        for(int i=0; i<m; ++i){
            for(int j=0; j<i; ++j){
                bool connect = false;
                for(int k=0; k<n; ++k){
                    if(segmentPointDist(p1[k], p2[k], p[i]) < EPS && segmentPointDist(p1[k], p2[k], p[j]) < EPS)
                        connect = true;
                }
                if(connect){
                    edges[i].push_back(j);
                    edges[j].push_back(i);
                }
            }
        }

        map<pair<int, int>, double> check;
        multimap<double, pair<int, int> > mm;
        for(unsigned i=0; i<edges[0].size(); ++i){
            check[make_pair(0, edges[0][i])] = 0.0;
            mm.insert(make_pair(0.0, make_pair(0, edges[0][i])));
        }

        double ret = -1.0;
        while(!mm.empty()){
            double cost = mm.begin()->first;
            int prev = mm.begin()->second.first;
            int curr = mm.begin()->second.second;
            mm.erase(mm.begin());
            if(cost > check[make_pair(prev, curr)] + EPS)
                continue;
            if(curr == 1){
                ret = cost;
                break;
            }

            for(unsigned i=0; i<edges[curr].size(); ++i){
                int next = edges[curr][i];
                double theta = (p[next] - p[curr]).dot(p[curr] - p[prev]) / (p[next] - p[curr]).length() / (p[curr] - p[prev]).length();
                theta = min(1.0, max(-1.0, theta));
                theta = acos(theta);
                double cost2 = cost + theta;

                if(check.find(make_pair(curr, next)) == check.end() || cost2 < check[make_pair(curr, next)] - EPS){
                    check[make_pair(curr, next)] = cost2;
                    mm.insert(make_pair(cost2, make_pair(curr, next)));
                }
            }
        }

        if(ret < -0.5){
            cout << -1 << endl;
        }else{
            ret *= 180 / PI;
            cout << fixed << setprecision(10) << ret << endl;
        }
    }
}