# Original solution
# -*- coding: utf-8 -*-
import sys
import io


def main():
    N = int( sys.stdin.readline() )

    XYU_list = []

    for _ in range(N):
        x,y,u = sys.stdin.readline().rstrip().split()
        XYU_list.append( [int(x),int(y),u] )

    
    check_UD = {}
    check_LR = {}

    check_UR = {}
    check_DL = {}
    check_UL = {}
    check_DR = {}


    for x,y,u in XYU_list:
        if (u == "U") or (u == "D"):
            check_UD.setdefault( x , [] )
            check_UD[x].append( (y,u) )
        
        if (u == "L") or (u == "R"):
            check_LR.setdefault( y , [] )
            check_LR[y].append( (x,u) )
        
        if (u == "U") or (u == "R"):
            check_UR.setdefault( (x+y) , [] )
            check_UR[ (x+y) ].append( (x,u) )

        if (u == "D") or (u == "L"):
            check_DL.setdefault( (x+y) , [] )
            check_DL[ (x+y) ].append( (x,u) )

        if (u == "U") or (u == "L"):
            check_UL.setdefault( (x-y) , [] )
            check_UL[ (x-y) ].append( (x,u) )

        if (u == "D") or (u == "R"):
            check_DR.setdefault( (x-y) , [] )
            check_DR[ (x-y) ].append( (x,u) )


    for check_dic in [check_UD, check_LR, check_UR, check_DL, check_UL, check_DR]:
        for key in check_dic:
            check_dic[key].sort()
    

    diff_min = float("inf")
    
    for arr in list(check_UD.values()):
        for i in range(len(arr) - 1):
            if (arr[i][1] == "U") and (arr[i+1][1] == "D"):
                diff_min = min( diff_min, (arr[i+1][0] - arr[i][0]) / 2 )
    
    for arr in list(check_LR.values()):
        for i in range(len(arr) - 1):
            if (arr[i][1] == "R") and (arr[i+1][1] == "L"):
                diff_min = min( diff_min, (arr[i+1][0] - arr[i][0]) / 2 )



    for arr in list(check_UR.values()):
        for i in range(len(arr) - 1):
            if (arr[i][1] == "R") and (arr[i+1][1] == "U"):
                diff_min = min( diff_min, (arr[i+1][0] - arr[i][0]) )
    
    for arr in list(check_DL.values()):
        for i in range(len(arr) - 1):
            if (arr[i][1] == "D") and (arr[i+1][1] == "L"):
                diff_min = min( diff_min, (arr[i+1][0] - arr[i][0]) )

    for arr in list(check_UL.values()):
        for i in range(len(arr) - 1):
            if (arr[i][1] == "U") and (arr[i+1][1] == "L"):
                diff_min = min( diff_min, (arr[i+1][0] - arr[i][0]) )

    for arr in list(check_DR.values()):
        for i in range(len(arr) - 1):
            if (arr[i][1] == "R") and (arr[i+1][1] == "D"):
                diff_min = min( diff_min, (arr[i+1][0] - arr[i][0]) )



    if diff_min != float("inf"):
        print(( int(diff_min * 10) ))
    else:
        print("SAFE")


if __name__ == "__main__":
    main()


# Optimized solution
# -*- coding: utf-8 -*-
import sys
import io


def main():
    N = int( sys.stdin.readline() )

    XYU_list = []

    for _ in range(N):
        x,y,u = sys.stdin.readline().rstrip().split()
        XYU_list.append( [int(x),int(y),u] )


    diff = float("inf")

    # check(1) rotate 0 degree
    ret = checkSafety_UD(XYU_list)
    diff = min(diff, ret)
    
    ret = checkSafety_UR(XYU_list)
    diff = min(diff, ret)
    
    # check(2) rotate 90 degree
    rotate(XYU_list)

    ret = checkSafety_UD(XYU_list)
    diff = min(diff, ret)
    
    ret = checkSafety_UR(XYU_list)
    diff = min(diff, ret)

    # check(3) rotate 180 degree
    rotate(XYU_list)

    ret = checkSafety_UR(XYU_list)
    diff = min(diff, ret)

    # check(4) rotate 270 degree
    rotate(XYU_list)

    ret = checkSafety_UR(XYU_list)
    diff = min(diff, ret)


    if diff != float("inf"):
        print(( int(diff * 10) ))
    else:
        print("SAFE")


def rotate(XYU_list :list):
    for i in range(len(XYU_list)):
        x,y,u = XYU_list[i][0], XYU_list[i][1], XYU_list[i][2]
        
        x2 = -y
        y2 = x
        
        if u == "U":  u2 = "L"
        if u == "D":  u2 = "R"
        if u == "L":  u2 = "D"
        if u == "R":  u2 = "U"
        
        XYU_list[i] = [x2,y2,u2]


def checkSafety_UD(XYU_list :list) -> int:
    check_UD = {}

    for x,y,u in XYU_list:
        if (u == "U") or (u == "D"):
            check_UD.setdefault( x , [] )
            check_UD[x].append( (y,u) )

    for key in check_UD:
        check_UD[key].sort()


    diff_min = float("inf")
    
    for arr in list(check_UD.values()):
        for i in range(len(arr) - 1):
            if (arr[i][1] == "U") and (arr[i+1][1] == "D"):
                diff_min = min( diff_min, (arr[i+1][0] - arr[i][0]) / 2 )

    return diff_min


def checkSafety_UR(XYU_list) -> int:
    check_UR = {}

    for x,y,u in XYU_list:
        if (u == "U") or (u == "R"):
            check_UR.setdefault( (x+y) , [] )
            check_UR[ (x+y) ].append( (x,u) )

    for key in check_UR:
        check_UR[key].sort()


    diff_min = float("inf")
    
    for arr in list(check_UR.values()):
        for i in range(len(arr) - 1):
            if (arr[i][1] == "R") and (arr[i+1][1] == "U"):
                diff_min = min( diff_min, (arr[i+1][0] - arr[i][0]) )

    return diff_min


if __name__ == "__main__":
    main()