import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Char8 as BS
import Data.List

readInt = fst . fromJust . BS.readInt
readIntList = map readInt . BS.words
getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine

main = do
    n <- getInt
    xyu <- fmap sort $ replicateM n $ do
        [x,y,u] <- words <$> getLine
        let x' = read x :: Int
        let y' = read y :: Int
        let u' = head u
        return (u',(x',y'))
    let (ds,ls,rs,us) = (map snd d, map snd l, map snd r, map snd u) 
            where (d,xs) = span ((=='D') . fst) xyu
                  (l,ys) = span ((=='L') . fst) xs
                  (r,u) = span ((=='R') . fst) ys
    let mRL = smin $ solveRL rs ls
        mRD = smin $ solveRD rs ds
        mRU = smin $ solveRU rs us
        mLD = smin $ solveLD ls ds
        mLU = smin $ solveLU ls us
        mUD = smin $ solveUD us ds
        a1 = minimum [mRL,mRD,mRU,mLD,mLU,mUD]
        ans | a1 == 1000000 = "SAFE"
            | otherwise = show $ a1 * 5
    putStrLn ans

solveRL [] _ = []
solveRL _ [] = []
solveRL ((rx,ry) :rs) ((lx,ly):ls) | ry < ly = solveRL rs ((lx,ly):ls)
                                   | ry > ly = solveRL ((rx,ry) :rs) ls
                                   | rx > lx = solveRL ((rx,ry) :rs) ls
                                   | otherwise = (lx - rx) : solveRL rs ((lx,ly):ls)

solveRD rs ds = solveRL rs' ds'
    where f (x,y) = (y+x,y-x)
          rs' = map f rs
          ds' = map f ds

solveRU rs us = solveRL rs' us'
    where f (x,y) = (x-y,x+y)
          rs' = map f rs
          us' = map f us

solveLD ls ds = solveRD ls' ds'
    where f (x,y) = (-x,y)
          ls' = map f ls
          ds' = map f ds

solveLU ls us = solveRU ls' us'
    where f (x,y) = (-x,y)
          ls' = map f ls
          us' = map f us

solveUD us ds = solveRL us' ds'
    where f (x,y) = (y,x)
          us' = map f us
          ds' = map f ds

smin xs | null xs = 1000000
        | otherwise = minimum xs