I=N=9**9

P,M,L,R,U,*D='x+y,','x-y,',[],[],[]

for t in[*open(0)][1:]:*z,u=t.split();exec(u+'+=[*map(int,z)],')

for*A,S in(D,U,'x,y,'),(L,R,'y,x,'),(U,R,P+M),(L,D,P+M),(L,U,M+P),(D,R,M+P):

 n=p=-I

 for x,y,r in eval('sorted((%sQ)for Q,P in enumerate(A)for x,y in P)'%S):

  if r:n,p=x,y

  elif x==n:N=min(N,y-p)

print((N%I*5or'SAFE'))
I=N=9**9

d=[[],[],[],[]]

for t in[*open(0)][1:]:

  *z,u=t.split()

  d[ord(u)%5]+=[*list(map(int,z))],

f=lambda x,y:x

g=lambda x,y:y

h=lambda x,y:x+y

i=lambda x,y:x-y

for*A,f,g in(3,0,f,g),(1,2,g,f),(0,2,h,i),(1,3,h,i),(1,0,i,h),(3,2,i,h):

  n=p=-I

  for x,y,r in sorted((f(x,y),g(x,y),Q)for Q,P in enumerate(A)for x,y in d[P]):

    if r:n,p=x,y

    elif x==n:N=min(N,y-p)

print((N%I*5or'SAFE'))
I = N = 9**9
P, M, L, R, U, *D = "x+y,", "x-y,", [], [], []
for t in [*open(0)][1:]:
    *z, u = t.split()
    exec(u + "+=[*map(int,z)],")
for *A, S in (
    (D, U, "x,y,"),
    (L, R, "y,x,"),
    (U, R, P + M),
    (L, D, P + M),
    (L, U, M + P),
    (D, R, M + P),
):
    n = p = -I
    for x, y, r in eval("sorted((%sQ)for Q,P in enumerate(A)for x,y in P)" % S):
        if r:
            n, p = x, y
        elif x == n:
            N = min(N, y - p)
print((N % I * 5 or "SAFE"))

I = N = 9**9
d = [[], [], [], []]
for t in [*open(0)][1:]:
    *z, u = t.split()
    d[ord(u) % 5] += ([*list(map(int, z))],)
f = lambda x, y: x
g = lambda x, y: y
h = lambda x, y: x + y
i = lambda x, y: x - y
for *A, f, g in (
    (3, 0, f, g),
    (1, 2, g, f),
    (0, 2, h, i),
    (1, 3, h, i),
    (1, 0, i, h),
    (3, 2, i, h),
):
    n = p = -I
    for x, y, r in sorted(
        (f(x, y), g(x, y), Q) for Q, P in enumerate(A) for x, y in d[P]
    ):
        if r:
            n, p = x, y
        elif x == n:
            N = min(N, y - p)
print((N % I * 5 or "SAFE"))

-P, M, L, R, U, *D = "x+y,", "x-y,", [], [], [] +d = [[], [], [], []] -    exec(u + "+=[*map(int,z)],") -for *A, S in ( -    (D, U, "x,y,"), -    (L, R, "y,x,"), -    (U, R, P + M), -    (L, D, P + M), -    (L, U, M + P), -    (D, R, M + P), +    d[ord(u) % 5] += ([*list(map(int, z))],) +f = lambda x, y: x +g = lambda x, y: y +h = lambda x, y: x + y +i = lambda x, y: x - y +for *A, f, g in ( +    (3, 0, f, g), +    (1, 2, g, f), +    (0, 2, h, i), +    (1, 3, h, i), +    (1, 0, i, h), +    (3, 2, i, h), -    for x, y, r in eval("sorted((%sQ)for Q,P in enumerate(A)for x,y in P)" % S): +    for x, y, r in sorted( +        (f(x, y), g(x, y), Q) for Q, P in enumerate(A) for x, y in d[P] +    ):