n = int(input())
A = [list(input().split()) for i in range(n)]
l = 2*10**5 + 5
# l = 500
# B = [[int(A[i][0]), int(A[i][1]), int(A[i][0])+int(A[i][1]), int(A[i][0])-int(A[i][1])+l, A[i][2]] for i in range(n)]

D = [[[] for j in range(2*l)] for i in range(4)]

# for i in range(4):
#     for j in range(n):
#         D[i][B[j][i]].append(B[j])

for i in range(n):
    x = int(A[i][0])
    y = int(A[i][1])
    nn = [x,y,A[i][2]]
    D[0][x].append(nn)
    D[1][y].append(nn)
    D[2][x+y].append(nn)
    D[3][x-y+l].append(nn)

# print(D)
import bisect
ans = float("inf")
for i in range(l):
    # print(D[1][i])
    LR = [[], []]
    for j in range(len(D[1][i])):
        
        if D[1][i][j][2] == "L":
            LR[0].append(D[1][i][j][0])
        if D[1][i][j][2] == "R":
            LR[1].append(D[1][i][j][0])

        # print(j,LR)
        for k in range(len(LR[1])):
            ind = bisect.bisect(LR[0], LR[1][k])
            if ind != len(LR[0]):
                ans = min(ans, (LR[0][ind]-LR[1][k]) * 5)

for i in range(l):
    # print(D[0][i])
    LR = [[], []]
    for j in range(len(D[0][i])):
        
        if D[0][i][j][2] == "U":
            LR[0].append(D[0][i][j][1])
        if D[0][i][j][2] == "D":
            LR[1].append(D[0][i][j][1])

        # print(j,LR)
        for k in range(len(LR[0])):
            ind = bisect.bisect(LR[1], LR[0][k])
            # print("ind",ind)
            if ind != len(LR[1]):
                ans = min(ans, (LR[1][ind]-LR[0][k]) * 5)

for i in range(2*l):
    # print(D[1][i])
    LR = [[], []]
    LR2 = [[], []]
    for j in range(len(D[2][i])):
        
        if D[2][i][j][2] == "L":
            LR[0].append(D[2][i][j][0])
        if D[2][i][j][2] == "D":
            LR[1].append(D[2][i][j][0])

        if D[2][i][j][2] == "U":
            LR2[0].append(D[2][i][j][0])
        if D[2][i][j][2] == "R":
            LR2[1].append(D[2][i][j][0])

        # print(j,LR)
        for k in range(len(LR[1])):
            ind = bisect.bisect(LR[0], LR[1][k])
            if ind != len(LR[0]):
                ans = min(ans, (LR[0][ind]-LR[1][k]) * 10)
        
        for k in range(len(LR2[1])):
            ind = bisect.bisect(LR2[0], LR2[1][k])
            if ind != len(LR2[0]):
                ans = min(ans, (LR2[0][ind]-LR2[1][k]) * 10)

for i in range(2*l):
    # print(D[1][i])
    LR = [[], []]
    LR2 = [[], []]
    for j in range(len(D[3][i])):
        
        if D[3][i][j][2] == "D":
            LR[0].append(D[3][i][j][1])
        if D[3][i][j][2] == "R":
            LR[1].append(D[3][i][j][1])

        if D[3][i][j][2] == "L":
            LR2[0].append(D[3][i][j][1])
        if D[3][i][j][2] == "U":
            LR2[1].append(D[3][i][j][1])

        # print(j,LR)
        for k in range(len(LR[1])):
            ind = bisect.bisect(LR[0], LR[1][k])
            if ind != len(LR[0]):
                ans = min(ans, (LR[0][ind]-LR[1][k]) * 10)
        
        for k in range(len(LR2[1])):
            ind = bisect.bisect(LR2[0], LR2[1][k])
            if ind != len(LR2[0]):
                ans = min(ans, (LR2[0][ind]-LR2[1][k]) * 10)


if ans == float("inf"):
    ans = "SAFE"
print(ans)


### FIXED CODE ###

n = int(input())
a = n//200
print(10-a)