#include<cstdio>
#include<vector>
#include<algorithm>
using std::vector;
struct Point{
    int x,y;
    int d;
};
vector<int>xleft[400005], xright[400005];
vector<int>yup[400005], ydown[400005];
vector<int>pleft[800005], pright[800005];
vector<int>nleft[800005], nright[800005];
int offset = 400000;

int getSmall(int u,vector<int>&a){
    int L = 0, R = a.size()-1;
    int res = -1;
    while(L<=R){
        int M = (L+R)/2;
        if(a[M]<=u){
            res = M;
            L = M+1;
        }
        else R = M-1;
    }
    return res;
}

int getBig(int u,vector<int>&a){
    int L = 0, R = a.size()-1;
    int res = -1;
    while(L<=R){
        int M = (L+R)/2;
        if(a[M]>=u){
            res = M;
            R = M-1;
        }
        else L = M+1;
    }
    return res;
}

int partOne(int n){
    int ans = -1;
    for(int i = 0; i <= 400000; i++){
        for(int down: ydown[i]){
            int index = getSmall(down-1,yup[i]);
            if(index!=-1){
                int up = yup[i][index];
                int time = (down-up)/2;
                if(ans==-1 || time<ans) ans = time;
            }
        }
    }
    for(int i = 0; i <= 400000; i++){
        for(int left: xleft[i]){
            int index = getSmall(left-1,xright[i]);
            if(index!=-1){
                int right = xright[i][index];
                int time = (left-right)/2;
                if(ans==-1 || time<ans) ans = time;
            }
        }
    }
    return ans;
}

int partTwo(int n){
    int ans = -1;
    for(int i = 0; i <= 400000; i++){
        for(int up: yup[i]){
            int pu = up+i;
            int index = getSmall(i-1,pright[pu]);
            if(index!=-1){
                int x = pright[pu][index];
                int time = i-x;
                if(ans==-1 || time<ans) ans = time;
            }
            int nu = up-i+offset;
            index = getBig(i+1,nleft[nu]);
            if(index!=-1){
                int x = nleft[nu][index];
                int time = x-i;
                if(ans==-1 || time<ans) ans = time;
            }
        }
        
        for(int down: ydown[i]){
            int pu = down+i;
            int index = getBig(i+1,pleft[pu]);
            if(index!=-1){
                int x = pleft[pu][index];
                int time = x-i;
                if(ans==-1 || time<ans) ans = time;
            }
            int nu = down-i+offset;
            index = getSmall(i-1,nright[nu]);
            if(index!=-1){
                int x = nright[nu][index];
                int time = i-x;
                if(ans==-1 || time<ans) ans = time;
            }
        }
    }
    return ans;
}

int main(){
    int n;
    scanf("%d",&n);
    for(int i = 1; i <= n; i++){
        int x,y;
        char s[2];
        scanf("%d%d%s",&x,&y,s);
        x *= 2, y *= 2; // *= 5 = answer
        if(s[0]=='U') yup[x].push_back(y);
        else if(s[0]=='D') ydown[x].push_back(y);
        else if(s[0]=='R'){ 
            pright[y+x].push_back(x);
            nright[y-x+offset].push_back(x);
            xright[y].push_back(x);
        }
        else{ 
            pleft[y+x].push_back(x);
            nleft[y-x+offset].push_back(x);
            xleft[y].push_back(x);
        }
    }
    for(int i = 0; i <= 800000; i++){
        if(i<=400000){
            std::sort(xleft[i].begin(),xleft[i].end());
            std::sort(xright[i].begin(),xright[i].end());
            std::sort(yup[i].begin(),yup[i].end());
            std::sort(ydown[i].begin(),ydown[i].end());
        }
        std::sort(pleft[i].begin(),pleft[i].end());
        std::sort(pright[i].begin(),pright[i].end());
        std::sort(nleft[i].begin(),nleft[i].end());
        std::sort(nright[i].begin(),nright[i].end());
    }
    int res0 = partOne(n), res1 = partTwo(n);
    if(res0==-1 && res1==-1) printf("SAFE\n");
    else if(res0==-1) printf("%d\n",res1*5);
    else if(res1==-1) printf("%d\n",res0*5);
    else{
        int ans = res0<res1?res0:res1;
        printf("%d\n",ans*5);
    }
    return 0;
}


Measured memory_bytes: 129664