#include <bits/stdc++.h>

using namespace std;
using ll = long long;

using pi = pair<int,int>;
using pic = pair<pi,char>;

int main() {
    int n;
    cin>>n;
    vector<vector<int>> migiue(500000);
    vector<vector<int>> migisita(500000);
    vector<vector<int>> ue(500000);
    vector<vector<int>> migi(500000);
    vector<pair<pi,char>> d(n);
    for (int i = 0; i < n; ++i) {
        int x,y;
        cin>>x>>y;
        char u;
        cin>>u;
        d[i]=make_pair(pi(x,y),u);
        migiue[y-x+250000].emplace_back(i);
        migisita[x+y].emplace_back(i);
        ue[x].emplace_back(i);
        migi[y].emplace_back(i);
    }
    int ans=5000000;
    for(auto v:migi) {
        if(v.size()<=1)continue;
        vector<pi> ve;
        for(auto z:v) {
            if(d[z].second=='R') {
                ve.emplace_back(d[z].first.first,0);
            }
            if(d[z].second=='L') {
                ve.emplace_back(d[z].first.first,1);
            }
        }
        sort(ve.begin(),ve.end());
        int l=-1,r=500000;
        for(auto z:ve) {
            if(z.second==0)r=z.first;
            if(z.second==1)l=z.first;
            if(r<l) {
                ans=min(ans,(l-r)*5);
                l=0;
                r=500000;
            }
        }
    }

    for(auto v:ue) {
        if(v.size()<=1)continue;
        vector<pi> ve;
        for(auto z:v) {
            if(d[z].second=='U') {
                ve.emplace_back(d[z].first.second,0);
            }
            if(d[z].second=='D') {
                ve.emplace_back(d[z].first.second,1);
            }
        }
        sort(ve.begin(),ve.end());
        int l=-1,r=500000;
        for(auto z:ve) {
            if(z.second==0)r=z.first;
            if(z.second==1)l=z.first;
            if(r<l) {
                ans=min(ans,(l-r)*5);
                l=-1;
                r=500000;
            }
        }
    }

    for(auto v:migiue) {
        if(v.size()<=1)continue;
        vector<pi> ve,ve2;
        for(auto z:v) {
            if(d[z].second=='R') {
                ve.emplace_back(d[z].first.first,0);
            }
            if(d[z].second=='D') {
                ve.emplace_back(d[z].first.first,1);
            }

            if(d[z].second=='U') {
                ve2.emplace_back(d[z].first.first,0);
            }
            if(d[z].second=='L') {
                ve2.emplace_back(d[z].first.first,1);
            }
        }
        sort(ve.begin(),ve.end());
        sort(ve2.begin(),ve2.end());
        int l=-1,r=500000;
        for(auto z:ve) {
            if(z.second==0)r=z.first;
            if(z.second==1)l=z.first;
            if(r<l) {
                ans=min(ans,(l-r)*10);
                l=-1;
                r=500000;
            }
        }

        l=-1,r=500000;
        for(auto z:ve2) {
            if(z.second==0)r=z.first;
            if(z.second==1)l=z.first;
            if(r<l) {
                ans=min(ans,(l-r)*10);
                l=-1;
                r=500000;
            }
        }
    }

    for(auto v:migisita) {
        if(v.size()<=1)continue;
        vector<pi> ve,ve2;
        for(auto z:v) {
            if(d[z].second=='R') {
                ve.emplace_back(d[z].first.first,0);
            }
            if(d[z].second=='U') {
                ve.emplace_back(d[z].first.first,1);
            }

            if(d[z].second=='D') {
                ve2.emplace_back(d[z].first.first,0);
            }
            if(d[z].second=='L') {
                ve2.emplace_back(d[z].first.first,1);
            }
        }
        sort(ve.begin(),ve.end());
        sort(ve2.begin(),ve2.end());
        int l=-1,r=500000;
        for(auto z:ve) {
            if(z.second==0)r=z.first;
            if(z.second==1)l=z.first;
            if(r<l) {
                ans=min(ans,(l-r)*10);
                l=-1;
                r=500000;
            }
        }

        l=-1,r=500000;
        for(auto z:ve2) {
            if(z.second==0)r=z.first;
            if(z.second==1)l=z.first;
            if(r<l) {
                ans=min(ans,(l-r)*10);
                l=-1;
                r=500000;
            }
        }
    }

    if(ans==5000000)cout<<"SAFE"<<endl;
    else cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 74640