#include <bits/stdc++.h>
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N,M,H,W,Q,K,A,B;
string S;
//const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
typedef pair<ll, ll> P;
const ll INF = (1LL<<58);

template<class T> bool chmin(T &a, const T &b){
    if(a > b) {a = b; return true;}
    else return false;
}
template<class T> bool chmax(T &a, const T &b){
    if(a < b) {a = b; return true;}
    else return false;
}

ll check(set<P> &s){
    ll last = -INF * 2, res = INF;
    for(P p : s){
        if(p.se){
            chmin(res, p.fi - last);
        }else{
            last = p.fi;
        }
    }
    return res;
}

int main() {
    const ll t2 = 200000;
    typedef vector<vector<set<P> > > mats;
    map<char, int> ctoi = {{'U', 0}, {'R', 1}, {'D', 2}, {'L', 3}};
    cin>>N;
    vec x(N), y(N), u(N);
    //0 : up
    mats memo(6, vector<set<P>>(400001));
    rep(i,N) {
        char c;
        cin>>x[i]>>y[i]>>c;
        u[i] = ctoi[c];
        if(u[i] < 2){
            memo[u[i]][u[i] ? y[i] : x[i]].emplace(u[i] ? x[i] : y[i], 0);
            memo[u[i] + 2][y[i] - x[i] + t2].emplace(y[i] + x[i], 0);
        }else{
            memo[u[i] - 2][u[i] - 2 ? y[i] : x[i]].emplace(u[i] - 2 ? x[i] : y[i], 1);
            memo[5 - u[i]][y[i] - x[i] + t2].emplace(y[i] + x[i], 1);
        }
        if(u[i] == 0){
            memo[4][y[i] + x[i]].emplace(y[i] - x[i], 0);
        }else if(u[i] == 1){
            memo[4][y[i] + x[i]].emplace(y[i] - x[i], 1);
        }else if(u[i] == 2){
            memo[5][y[i] + x[i]].emplace(y[i] - x[i], 1);
        }else{
            memo[5][y[i] + x[i]].emplace(y[i] - x[i], 0);
        }
    }
    ll res = INF;
    rep(i,6){
        rep(j, 400001){
            chmin(res, check(memo[i][j]));
        }
    }
    if(res == INF) cout<<"SAFE"<<endl;
    else cout<<res * 5<<endl;
}

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;

#define REP(i, n, m) for(ll i=n; i<(ll)m; ++i)
#define IREP(i, n, m) for(ll i=n-1; i>=m; --i)
#define rep(i, n) REP(i, 0, n)
#define irep(i, n) IREP(i, n, 0)
#define all(v) v.begin(), v.end()
#define vprint(v) for(auto e:v){cout<<e<<" ";};cout<<endl;
#define vvprint(vv) for(auto v:vv)vprint(v);

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(20);

    ll H, W;
    cin >> H >> W;
    vll A(H), B(H);
    rep(i, H) cin >> A[i] >> B[i];

    map<int, int> m;
    rep(i, W) m[i] = 0;
    multiset<int> ms;
    rep(i, W) ms.insert(0);
    vll ans(H, 0);
    rep(i, H){
        int a = A[i]-1;
        int b = B[i]-1;
        auto lb = m.lower_bound(a);
        auto ub = m.upper_bound(b);
        if(lb!=m.end() && lb!=ub){
            --ub;
            auto it = lb;
            int k = ub->first;
            int v = ub->second;
            while(it!=m.end() && it->first!=k){
                int v = it->second;
                ms.erase(ms.find(v));
                it = m.erase(it);
            }
            if(b<W-1 && m.count(b+1)==0){
                m[b+1] = v+b-k+1;
                ms.insert(v+b-k+1);
            }
            m.erase(k);
            ms.erase(ms.find(v));
        }
        if(ms.empty()) ans[i] = -1;
        else ans[i] = *ms.begin() + i + 1;
    }
    rep(i, H) cout << ans[i] << endl;
}
