#include <bits/stdc++.h>

struct Add
{
    int x, y, add;
    Add(int _x, int _y): x(_x), y(_y), add(x+y) {}
    bool operator< (const Add& o) const
    {
        if (add != o.add)
            return add < o.add;
        return x < o.x;
    }
};

struct Sub
{
    int x, y, sub;
    Sub(int _x, int _y): x(_x), y(_y), sub(x-y) {}
    bool operator< (const Sub& o) const
    {
        if (sub != o.sub)
            return sub < o.sub;
        return x < o.x;
    }
};

struct X
{
    int x, y;
    X(int _x, int _y): x(_x), y(_y) {}
    bool operator< (const X& o) const
    {
        if (x != o.x)
            return x < o.x;
        return y < o.y;
    }
};

struct Y
{
    int x, y;
    Y(int _x, int _y): x(_x), y(_y) {}
    bool operator< (const Y& o) const
    {
        if (y != o.y)
            return y < o.y;
        return x < o.x;
    }
};

int main()
{
    int x, y;
    char d[2];
    int ans = -1;
    std::set<Add>
        UAdd, DAdd, LAdd, RAdd;
    std::set<Sub>
        USub, DSub, LSub, RSub;
    std::set<X>
        UX, DX;
    std::set<Y>
        LY, RY;
    int N;
    auto upd = [&](int x) -> void
    {
        if (ans == -1 || ans > x)
            ans = x;
    };
    scanf("%d", &N);
    for (int i = 0; i < N; ++i)
    {
        scanf("%d %d %s", &x, &y, d);
        if (d[0] == 'U')
        {
            UAdd.emplace(x, y); USub.emplace(x, y); UX.emplace(x, y);
            std::set<Add>::iterator i1 = RAdd.lower_bound({x, y});
            if (i1 != RAdd.begin())
                if ((--i1)->add == x + y)
                    upd((i1->y - y) * 10);
            std::set<Sub>::iterator i2 = LSub.lower_bound({x, y});
            if (i2 != LSub.end())
                if (i2->sub == x - y)
                    upd((i2->y - y) * 10);
            std::set<X>::iterator i3 = DX.lower_bound({x, y});
            if (i3 != DX.end())
                if (i3->x == x)
                    upd((i3->y - y) * 5);
        }
        else if (d[0] == 'D')
        {
            DAdd.emplace(x, y); DSub.emplace(x, y); DX.emplace(x, y);
            std::set<Sub>::iterator i1 = RSub.lower_bound({x, y});
            if (i1 != RSub.begin())
                if ((--i1)->sub == x - y)
                    upd((y - i1->y) * 10);
            std::set<Add>::iterator i2 = LAdd.lower_bound({x, y});
            if (i2 != LAdd.end())
                if (i2->add == x + y)
                    upd((y - i2->y) * 10);
            std::set<X>::iterator i3 = UX.lower_bound({x, y});
            if (i3 != UX.begin())
                if ((--i3)->x == x)
                    upd((y - i3->y) * 5);
        }
        else if (d[0] == 'L')
        {
            LAdd.emplace(x, y); LSub.emplace(x, y); LY.emplace(x, y);
            std::set<Add>::iterator i1 = DAdd.lower_bound({x, y});
            if (i1 != DAdd.begin())
                if ((--i1)->add == x + y)
                    upd((x - i1->x) * 10);
            std::set<Sub>::iterator i2 = USub.lower_bound({x, y});
            if (i2 != USub.begin())
                if ((--i2)->sub == x - y)
                    upd((x - i2->x) * 10);
            std::set<Y>::iterator i3 = RY.lower_bound({x, y});
            if (i3 != RY.begin())
                if ((--i3)->y == y)
                    upd((x - i3->x) * 5);
        }
        else // d[0] == 'R'
        {
            RAdd.emplace(x, y); RSub.emplace(x, y); RY.emplace(x, y);
            std::set<Sub>::iterator i1 = DSub.lower_bound({x, y});
            if (i1 != DSub.end())
                if (i1->sub == x - y)
                    upd((i1->x - x) * 10);
            std::set<Add>::iterator i2 = UAdd.lower_bound({x, y});
            if (i2 != UAdd.end())
                if (i2->add == x + y)
                    upd((i2->x - x) * 10);
            std::set<Y>::iterator i3 = LY.lower_bound({x, y});
            if (i3 != LY.end())
                if (i3->y == y)
                    upd((i3->x - x) * 5);
        }
    }
    if (ans == -1)
        printf("SAFE\n");
    else
        printf("%d\n", ans);
}

Measured memory_bytes: 38068