_,*a=list(map(str.split,open(0)))

U,R,D,L="URDL"

d={i:[]for i in"URDL"}

for *z,u in a:d[u]+=[*list(map(int,z))],

N=9e9

P,M="x+y,","x-y,"

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

 n=p=-9e9

 for x,y,r in sorted(eval(f"[({S}Q)for P,Q in[(A,1),(B,0)]for x,y in d[P]]")):

  if x-n:

   if r:n,p=x,y

  elif r:p=y

  else:N=min(N,y-p)

print((["SAFE",N*5][N!=9e9]))
_,*a=list(map(str.split,open(0)))

U,R,D,L="URDL"

d={i:[]for i in"URDL"}

for *z,u in a:d[u]+=[*list(map(int,z))],

N=9e9

P,M="x+y,","x-y,"

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

 n=p=-9e9

 for x,y,r in sorted(eval(f"[({S}Q)for P,Q in[(A,1),(B,0)]for x,y in d[P]]")):

  if x-n:

   if r:n,p=x,y

  elif r:p=y

  else:N=min(N,y-p)

print((["SAFE",N*5][N<9e9]))
_, *a = list(map(str.split, open(0)))
U, R, D, L = "URDL"
d = {i: [] for i in "URDL"}
for *z, u in a:
    d[u] += ([*list(map(int, z))],)
N = 9e9
P, M = "x+y,", "x-y,"
for A, B, S in (
    (U, D, "x,y,"),
    (R, L, "y,x,"),
    (R, U, P + M),
    (D, L, P + M),
    (U, L, M + P),
    (R, D, M + P),
):
    n = p = -9e9
    for x, y, r in sorted(eval(f"[({S}Q)for P,Q in[(A,1),(B,0)]for x,y in d[P]]")):
        if x - n:
            if r:
                n, p = x, y
        elif r:
            p = y
        else:
            N = min(N, y - p)
print((["SAFE", N * 5][N != 9e9]))

_, *a = list(map(str.split, open(0)))
U, R, D, L = "URDL"
d = {i: [] for i in "URDL"}
for *z, u in a:
    d[u] += ([*list(map(int, z))],)
N = 9e9
P, M = "x+y,", "x-y,"
for A, B, S in (
    (U, D, "x,y,"),
    (R, L, "y,x,"),
    (R, U, P + M),
    (D, L, P + M),
    (U, L, M + P),
    (R, D, M + P),
):
    n = p = -9e9
    for x, y, r in sorted(eval(f"[({S}Q)for P,Q in[(A,1),(B,0)]for x,y in d[P]]")):
        if x - n:
            if r:
                n, p = x, y
        elif r:
            p = y
        else:
            N = min(N, y - p)
print((["SAFE", N * 5][N < 9e9]))

-print((["SAFE", N * 5][N != 9e9])) +print((["SAFE", N * 5][N < 9e9]))