# Fast IO (only use in integer input)

import os,io
input=io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

N = int(input())
plane = []
for _ in range(N):
    xb,yb,ub = input().split()
    x = int(xb)
    y = int(yb)
    if ub == b'U':
        u = 'U'
    elif ub == b'R':
        u = 'R'
    elif ub == b'D':
        u = 'D'
    else:
        u = 'L'
    plane.append((x,y,u))

# UD, LR collision
XR = []
XL = []
YU = []
YD = []
for i in range(200001):
    XR.append([])
    XL.append([])
    YU.append([])
    YD.append([])
for elem in plane:
    if elem[2] == 'U':
        YU[elem[0]].append(elem[1])
    if elem[2] == 'D':
        YD[elem[0]].append(elem[1])
    if elem[2] == 'L':
        XL[elem[1]].append(elem[0])
    if elem[2] == 'R':
        XR[elem[1]].append(elem[0])

        
for i in range(200001):
    XR[i].sort()
    XL[i].sort()
    YU[i].sort()
    YD[i].sort()

minTime = 4000000
for i in range(200001):
    indexR = 0
    indexL = 0
    while indexR < len(XR[i]) and indexL < len(XL[i]):
        if XR[i][indexR] > XL[i][indexL]:
            indexL += 1
        else:
            if (XL[i][indexL] - XR[i][indexR]) * 5 < minTime:
                minTime = (XL[i][indexL] - XR[i][indexR]) * 5
            indexR += 1
    
    indexU = 0
    indexD = 0
    while indexU < len(YU[i]) and indexD < len(YD[i]):
        if YU[i][indexU] > YD[i][indexD]:
            indexD += 1
        else:
            if (YD[i][indexD] - YU[i][indexU]) * 5 < minTime:
                minTime = (YD[i][indexD] - YU[i][indexU]) * 5
            indexU += 1

# Other collisions

XYSumR = []
XYSumU = []
XYSumD = []
XYSumL = []
# Diff is X - Y + 200000
XYDiffR = []
XYDiffU = []
XYDiffD = []
XYDiffL = []

for i in range(400001):
    XYSumR.append([])
    XYSumU.append([])
    XYSumD.append([])
    XYSumL.append([])
    XYDiffR.append([])
    XYDiffU.append([])
    XYDiffD.append([])
    XYDiffL.append([])
for elem in plane:
    if elem[2] == 'U':
        XYSumU[elem[0] + elem[1]].append(elem[0])
        XYDiffU[elem[0] - elem[1] + 200000].append(elem[0])
    if elem[2] == 'D':
        XYSumD[elem[0] + elem[1]].append(elem[0])
        XYDiffD[elem[0] - elem[1] + 200000].append(elem[0])
    if elem[2] == 'L':
        XYSumL[elem[0] + elem[1]].append(elem[0])
        XYDiffL[elem[0] - elem[1] + 200000].append(elem[0])
    if elem[2] == 'R':
        XYSumR[elem[0] + elem[1]].append(elem[0])
        XYDiffR[elem[0] - elem[1] + 200000].append(elem[0])

for i in range(400001):
    XYSumU[i].sort()
    XYDiffU[i].sort()
    XYSumD[i].sort()
    XYDiffD[i].sort()
    XYSumL[i].sort()
    XYDiffL[i].sort()
    XYSumR[i].sort()
    XYDiffR[i].sort()

for i in range(400001):
    indexR = 0
    indexU = 0
    while indexR < len(XYSumR[i]) and indexU < len(XYSumU[i]):
        if XYSumU[i][indexU] > XYSumR[i][indexR]:
            if (XYSumU[i][indexU] - XYSumR[i][indexR]) * 10 < minTime:
                minTime = (XYSumU[i][indexU] - XYSumR[i][indexR]) * 10
            indexR += 1
        else:
            indexU += 1
    indexD = 0
    indexL = 0
    while indexL < len(XYSumL[i]) and indexD < len(XYSumD[i]):
        if XYSumD[i][indexD] > XYSumL[i][indexL]:
            indexL += 1
        else:
            if (XYSumL[i][indexL] - XYSumD[i][indexD]) * 10 < minTime:
                minTime = (XYSumL[i][indexL] - XYSumD[i][indexD]) * 10
            indexD += 1    
    indexL = 0
    indexU = 0
    while indexL < len(XYDiffL[i]) and indexU < len(XYDiffU[i]):
        if XYDiffU[i][indexU] > XYDiffL[i][indexL]:
            indexL += 1
        else:
            if (XYDiffL[i][indexL] - XYDiffU[i][indexU]) * 10 < minTime:
                minTime = (XYDiffL[i][indexL] - XYDiffU[i][indexU]) * 10
            indexU += 1
    indexR = 0
    indexD = 0
    while indexR < len(XYDiffR[i]) and indexD < len(XYDiffD[i]):
        if XYDiffR[i][indexR] > XYDiffD[i][indexD]:
            indexD += 1
        else:
            if (XYDiffD[i][indexD] - XYDiffR[i][indexR]) * 10 < minTime:
                minTime = (XYDiffD[i][indexD] - XYDiffR[i][indexR]) * 10
            indexR += 1

if minTime == 4000000:
    print("SAFE")
else:
    print(minTime)
    
   

Measured memory_bytes: 315472