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 <iostream>
#include <cassert>
#include <vector>
#include <set>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <complex>
#include <cmath>

using namespace std;
typedef long long ll;

typedef long double R;
typedef complex<R> P;

namespace std {
    bool operator < (P a, P b) {
        return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
    }
    bool operator > (P a, P b) {
        return (b < a);
    }
}

const R EPS = 1e-7;
const R PI = acos((R)(-1));

/*
 -1 -> neg
  0 -> near 0
  1 -> pos
  */
int sgn(R a) {
    if (a < -EPS) return -1;
    if (a > EPS) return 1;
    return 0;
}

int sgn(R a, R b) {
    return sgn(b-a);
}

bool near(P a, P b) {
    return !sgn(abs(a-b));
}

R cross(P a, P b) { return a.real()*b.imag() - a.imag()*b.real(); }
R dot(P a, P b) { return a.real()*b.real() + a.imag()*b.imag(); }

R ssqrt(R d) {
    d = max<R>(0, d);
    return sqrt(d);
}

R sacos(R d) {
    d = max<R>(-1, d);
    d = min<R>(1, d);
    return acos(d);
}


R deg2rad(R x) {
    return x/180*PI;
}

R rad2deg(R x) {
    return x/PI*180;
}

//?§???????[0, 2*PI)???
R radNorP(R x) {
    return fmod(fmod(x, 2*PI) + 2*PI, 2*PI);
}

//?§???????[-PI, PI)???
R radNorN(R x) {
    x = radNorP(x);
    if (x >= PI) x -= 2*PI;
    return x;
}

/**
 * radian??§???x???[l, r]?????\??£????????????????????\??????
 * 0:OFF
 * 1:IN
 * 2:ON
 */
bool inR(R l, R r, R x) {
    l = radNorP(l);
    r = radNorP(r);
    x = radNorP(x);
    if (!sgn(l, x) || !sgn(r, x)) return 2;
    if (!sgn(l, r)) return 0;
    if (sgn(l, r) == 1) {
        if (sgn(l, x) == 1 && sgn(x, r) == 1) return 1;
    } else {
        if (sgn(x, r) == 1 || sgn(l, x) == 1) return 1;
    }
    return 0;
}


/* 1->cclock
  -1->clock
   0->on
   2->back
  -2->front
  */
int ccw(P a, P b, P c) {
    assert(!near(a, b));
    if (near(a, c) || near(b, c)) return 0;
    int s = sgn(cross(b-a, c-a));
    if (s) return s;
    if (dot(b-a, c-a) < 0) return 2;
    if (dot(a-b, c-b) < 0) return -2;
    return 0;
}

struct L {
    P x, y;
    L() {};
    L(P x, P y) :x(x), y(y) {};
};

P vec(const L &l) {
    return l.y - l.x;
}

R abs(const L &l) {
    return abs(vec(l));
}

int crossLL(const L &l, const L &m, P &r) {
    L mm = L(m.x - l.x, m.y - l.x);
    mm.x *= polar<R>(1.0, -arg(vec(l)));
    mm.y *= polar<R>(1.0, -arg(vec(l)));
    if (sgn(vec(mm).imag()) == 0) {
        if (sgn(mm.x.imag()) == 0) return -1;
        return 0;
    }
    r = mm.x - vec(mm) * (mm.x.imag() / vec(mm).imag());
    r *= polar<R>(1.0, arg(vec(l)));
    r += l.x;
    return 1;
}

int crossSS(const L &l, const L &m, P &r) {
    int u = crossLL(l, m, r);
    if (u == 0) return 0;
    if (u == -1) {
        int x = ccw(l.x, l.y, m.x);
        int y = ccw(l.x, l.y, m.y);
        if (x == 0) {
            r = m.x;
            return -1;
        }
        if (y == 0) {
            r = m.y;
            return -1;
        }
        if (x == y) return 0;
        r = l.x;
        return -1;
    }
    if (ccw(l.x, l.y, r) == 0 && ccw(m.x, m.y, r) == 0) return 1;
    return 0;
}

/*
exec??§res??????????????¢?????\???
*/
const R INF = 1e12;
template<int V>
struct Dijkstra {
    typedef R T;
    typedef pair<T, int> P;
    vector<P> g[V];
    void init() {
        for (int i = 0; i < V; i++) {
            g[i].clear();
        }
    }
    void add(int from, int to, T dist) {
        g[from].push_back(P(dist, to));
    }
    T res[V];
    void exec(int s) {
        fill_n(res, V, INF);
        priority_queue<P, vector<P>, greater<P>> q;
        q.push(P(0, s));
        res[s] = 0;
        while (!q.empty()) {
            P p = q.top(); q.pop();
            if (res[p.second] < p.first) continue;
            for (P e: g[p.second]) {
                if (p.first+e.first < res[e.second]) {
                    res[e.second] = p.first+e.first;
                    q.push(P(e.first+p.first, e.second));
                }
            }
        }
        return;
    }
};


typedef pair<P, int> E;
const int MN = 55;
Dijkstra<MN*MN*2> djk;
int n;
L l[MN];
R ar[MN];
P s, t;
vector<E> g[MN];
int xyd2id(int x, int y, int d) {
    return (y*MN+x)*2+d;
}
R calc() {
    djk.init();
    for (int i = 0; i < MN; i++) {
        g[i].clear();
    }
    for (int i = 0; i < n; i++) {
        ar[i] = arg(vec(l[i]));
    }
    for (int i = 0; i < n; i++) {
        for (int j = i+1; j < n; j++) {
            P p;
            int u = crossSS(l[i], l[j], p);
            if (u == 0) continue;
//            printf("cross %d %d\n", i, j);
            g[i].push_back(E(p, j));
            g[j].push_back(E(p, i));
            djk.add(xyd2id(i, j, 0), xyd2id(j, i, 0), abs(radNorN(ar[i]-ar[j])));
            djk.add(xyd2id(i, j, 0), xyd2id(j, i, 1), PI-abs(radNorN(ar[i]-ar[j])));
            djk.add(xyd2id(i, j, 1), xyd2id(j, i, 0), PI-abs(radNorN(ar[i]-ar[j])));
            djk.add(xyd2id(i, j, 1), xyd2id(j, i, 1), abs(radNorN(ar[i]-ar[j])));
            djk.add(xyd2id(j, i, 0), xyd2id(i, j, 0), abs(radNorN(ar[i]-ar[j])));
            djk.add(xyd2id(j, i, 0), xyd2id(i, j, 1), PI-abs(radNorN(ar[i]-ar[j])));
            djk.add(xyd2id(j, i, 1), xyd2id(i, j, 0), PI-abs(radNorN(ar[i]-ar[j])));
            djk.add(xyd2id(j, i, 1), xyd2id(i, j, 1), abs(radNorN(ar[i]-ar[j])));
        }
    }
    for (int i = 0; i < n; i++) {
        if (ccw(l[i].x, l[i].y, s) == 0) {
//            printf("start %d\n", i);
            g[i].push_back(E(s, MN-2));
            djk.add(xyd2id(MN-2, MN-2, 0), xyd2id(i, MN-2, 0), 0);
            djk.add(xyd2id(MN-2, MN-2, 0), xyd2id(i, MN-2, 1), 0);
        }
    }
    for (int i = 0; i < n; i++) {
        if (ccw(l[i].x, l[i].y, t) == 0) {
//            printf("end %d\n", i);
            g[i].push_back(E(t, MN-1));
            djk.add(xyd2id(i, MN-1, 0), xyd2id(MN-1, MN-1, 0), 0);
            djk.add(xyd2id(i, MN-1, 1), xyd2id(MN-1, MN-1, 0), 0);
        }
    }
    for (int i = 0; i < n; i++) {
        sort(g[i].begin(), g[i].end(), [&](const E &x, const E &y){
            return abs(x.first-l[i].x) < abs(y.first-l[i].x);
        });
        for (int j = 0; j < (int)g[i].size() - 1; j++) {
            int x = g[i][j].second, y = g[i][j+1].second;
            djk.add(xyd2id(i, x, 0), xyd2id(i, y, 0), 0);
            djk.add(xyd2id(i, y, 1), xyd2id(i, x, 1), 0);
            if (sgn(abs(g[i][j].first-l[i].x), abs(g[i][j+1].first-l[i].x)) == 0) {
                djk.add(xyd2id(i, x, 1), xyd2id(i, y, 1), 0);
                djk.add(xyd2id(i, y, 0), xyd2id(i, x, 0), 0);
            }
        }
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i == j) continue;
            djk.add(xyd2id(i, j, 0), xyd2id(i, j, 1), PI);
        }
    }
    djk.exec(xyd2id(MN-2, MN-2, 0));
    R res = djk.res[xyd2id(MN-1, MN-1, 0)];
    if (res > INF/2) {
        return -1;
    }
    return rad2deg(res);
}

int main() {
    while (true) {
        cin >> n;
        if (!n) break;
        for (int i = 0; i < n; i++) {
            R x1, y1, x2, y2;
            cin >> x1 >> y1 >> x2 >> y2;
            l[i] = L(P(x1, y1), P(x2, y2));
        }
        R sx, sy, tx, ty;
        cin >> sx >> sy >> tx >> ty;
        s = P(sx, sy);
        t = P(tx, ty);
        printf("%.20Lf\n", calc());
    }
    return 0;
}