Problem: Prof. Jenifer A. Gibson is carrying out experiments with many robots. Since those robots are expensive, she wants to avoid their crashes during her experiments at her all effort. So she asked you, her assistant, as follows.

“Suppose that we have n (2 ≤ n ≤ 100000) robots of the circular shape with the radius of r, and that they are placed on the xy-plane without overlaps. Each robot starts to move straight with a velocity of either v or -v simultaneously, say, at the time of zero. The robots keep their moving infinitely unless I stop them. I’d like to know in advance if some robots will crash each other. The robots crash when their centers get closer than the distance of 2r. I’d also like to know the time of the first crash, if any, so I can stop the robots before the crash. Well, could you please write a program for this purpose?”



Input

The input consists of multiple datasets. Each dataset has the following format:


n
vx vy
r
rx1 ry1 u1
rx2 ry2 u2
...
rxn ryn un


n is the number of robots. (vx, vy) denotes the vector of the velocity v. r is the radius of the robots. (rxi , ryi ) denotes the coordinates of the center of the i-th robot. ui denotes the moving direction of the i-th robot, where 1 indicates the i-th robot moves with the velocity (vx , vy), and -1 indicates (-vx , -vy).

All the coordinates range from -1200 to 1200. Both vx and vy range from -1 to 1.

You may assume all the following hold for any pair of robots:

* they must not be placed closer than the distance of (2r + 10-8 ) at the initial state;
* they must get closer than the distance of (2r - 10-8 ) if they crash each other at some point of time; and
* they must not get closer than the distance of (2r + 10-8 ) if they don’t crash.



The input is terminated by a line that contains a zero. This should not be processed.

Output

For each dataset, print the first crash time in a line, or “SAFE” if no pair of robots crashes each other. Each crash time should be given as a decimal with an arbitrary number of fractional digits, and with an absolute error of at most 10-4.

Example

Input

2
1.0 0.0
0.5
0.0 0.0 1
2.0 0.0 -1
2
1.0 0.0
0.5
0.0 0.0 -1
2.0 0.0 1
0


Output

0.500000
SAFE

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
#define REP(i,n) for(int i=0; i<(int)(n); ++i)

using namespace std;

typedef complex<double> Point;
namespace std{
    bool operator < (const Point& p1, const Point& p2){
        if(p1.real() != p2.real()) return p1.real() < p2.real();
        return p1.imag() < p2.imag();
    }
};
struct Event{
    double y;
    Point p;
    int p_type;
    int e_type;
    Event(double y, Point p, int p_type, int e_type) : 
        y(y), p(p), p_type(p_type), e_type(e_type) {}
    bool operator < (const Event& e) const{
        if(y != e.y) return y < e.y;
        if(e_type != e.e_type) return e_type > e.e_type; // delete is first ???
        return p < e.p;
    }
};


double get_time(Point p, Point q, double R, double v){
    double dx = abs(p.real() - q.real());
    double dy = abs(p.imag() - q.imag());
    if(!(2 * R >= dy)) return 1e16;
    assert(2 * R >= dy);
    double b = sqrt(4 * R * R - dy * dy);
    assert(dx - b >= 0);
    return (dx - b) / v;
}

void update(double& ans, vector<Point>& pset, Point p, double R, double v, int t){
    int k = lower_bound(pset.begin(), pset.end(), p) - pset.begin();
    for(int dk = -1; dk <= 1; dk++) if(k + dk >= 0 && k + dk < pset.size()){
        if(t == 0 && p.real() < pset[k + dk].real()){
            ans = min(ans, get_time(p, pset[k + dk], R, v));
        }else if(t == 1 && p.real() > pset[k + dk].real()){
            ans = min(ans, get_time(p, pset[k + dk], R, v));
        }
    }
}

int main(){
    int N;
    while(cin >> N && N){
        double vx, vy;
        cin >> vx >> vy;
        vx *= 2; vy *= 2;
        double th = arg(Point(vx, vy));
        double v = sqrt(vx * vx + vy * vy);
        double R;
        cin >> R;

        vector<Event> events;

        REP(i, N){
            double x, y;
            int u;
            cin >> x >> y >> u;
            Point p(x, y);
            p = p * polar(1.0, -th);
            if(u == 1){
                events.push_back(Event(p.imag() - R, p, 0, 0));
                events.push_back(Event(p.imag() + R, p, 0, 1));
            }else{
                events.push_back(Event(p.imag() - R, p, 1, 0));
                events.push_back(Event(p.imag() + R, p, 1, 1));
            }
        }

        sort(events.begin(), events.end());

        vector<Point> point_set[2];
        double ans = 1e16;
        for(int i = 0; i < events.size(); i++){
            Event e = events[i];
            //cout << e.p << " " << e.e_type << " " << e.p_type << endl;
            vector<Point>& my_set = point_set[e.p_type];
            vector<Point>& you_set = point_set[e.p_type ^ 1];
            if(e.e_type == 0){
                my_set.insert(lower_bound(my_set.begin(), my_set.end(), e.p), e.p);
                update(ans, you_set, e.p, R, v, e.p_type);
            }else if(e.e_type == 1){
                my_set.erase(lower_bound(my_set.begin(), my_set.end(), e.p));
                int k = lower_bound(you_set.begin(), you_set.end(), e.p) - you_set.begin();
                for(int dk = -1; dk <= 1; dk ++) if(k + dk >= 0 && k + dk < you_set.size()){
                    update(ans, my_set, you_set[k], R, v, e.p_type ^ 1);
                }
            }
        }
        if(ans >= 1e16){
            puts("SAFE");
        }else{
            printf("%.6f\n", ans);
        }
    }
    return 0;
}