CodeEff/ECCO huggingface dataset:
# Original solution
from bisect import bisect_left
M = 200001
n = int(eval(input()))
l = [None] * n
ans = 10 ** 18
for i in range(n):
    x, y, u = input().split()
    u = ('R', 'D', 'L', 'U').index(u)
    l[i] = (int(x), int(y), u)
for _ in range(4):
    l.sort()
    xy = [[] for _ in range(M * 2 - 1)]
    yx = [[] for _ in range(M * 2 - 1)]
    xs = [[] for _ in range(M)]
    k = []
    for i, t in enumerate(l):
        x, y, u = t
        u %= 4
        if u == 3:
            xy[x + y].append(x)
        elif u == 1:
            yx[x - y + M - 1].append(x)
        elif u == 2:
            xs[y].append(x)
        else:
            k.append((x, y))
        l[i] = (y, -x + M - 1, u + 1)
    for x, y in k:
        idx = bisect_left(xs[y], x)
        if idx < len(xs[y]):
            ans = min(ans, (xs[y][idx] - x) * 5)
        idx = bisect_left(xy[x + y], x)
        if idx < len(xy[x + y]):
            ans = min(ans, (xy[x + y][idx] - x) * 10)
        idx = bisect_left(yx[x - y + M - 1], x)
        if idx < len(yx[x - y + M - 1]):
            ans = min(ans, (yx[x - y + M - 1][idx] - x) * 10)
if ans == 10 ** 18:
    print('SAFE')
else:
    print(ans)


# Optimized solution
from bisect import bisect_left
import sys
input = sys.stdin.readline
M = 200001
n = int(eval(input()))
l = [None] * n
ans = 10 ** 18
for i in range(n):
    x, y, u = input().split()
    u = ('R', 'D', 'L', 'U').index(u)
    l[i] = (int(x), int(y), u)
for _ in range(3):
    l.sort()
    xy = [[] for _ in range(M * 2 - 1)]
    yx = [[] for _ in range(M * 2 - 1)]
    xs = [[] for _ in range(M)]
    k = []
    for i, t in enumerate(l):
        x, y, u = t
        u %= 4
        if u == 3:
            xy[x + y].append(x)
        elif u == 1:
            yx[x - y + M - 1].append(x)
        elif u == 2:
            xs[y].append(x)
        elif u == 0:
            k.append((x, y))
        l[i] = (y, -x + M - 1, u + 1)
    for x, y in k:
        idx = bisect_left(xs[y], x)
        if idx < len(xs[y]):
            ans = min(ans, (xs[y][idx] - x) * 5)
        idx = bisect_left(xy[x + y], x)
        if idx < len(xy[x + y]):
            ans = min(ans, (xy[x + y][idx] - x) * 10)
        idx = bisect_left(yx[x - y + M - 1], x)
        if idx < len(yx[x - y + M - 1]):
            ans = min(ans, (yx[x - y + M - 1][idx] - x) * 10)
if ans == 10 ** 18:
    print('SAFE')
else:
    print(ans)
# Original solution
def get_collision(A,ans):
    A.sort()
    nowkey = -10**10
    prevcoor = -10**10
    ret = ans[0]
    for key, coor, a in A:
        if key != nowkey:
            if a == 1:
                nowkey = key
                prevcoor = coor
                continue
            else:
                continue
        if a == 1:
            prevcoor = coor
            continue
        if (coor - prevcoor)*5 < ret:
            ret = (coor - prevcoor)*5
    ans[0] = ret

def main():
    N = int( eval(input()))
    XYU = [ tuple( input().split()) for _ in range(N)]
    U = []
    R = []
    D = []
    L = []
    for x, y, u in XYU:
        x, y = int(x), int(y)
        if u == "U":
            U.append((x,y))
        elif u == "D":
            D.append((x,y))
        elif u == "L":
            L.append((x,y))
        else:
            R.append((x,y))
    ans = [10**10]

    UD = [(x,y,1) for x, y in U] + [(x,y,-1) for x, y in D]
    get_collision(UD, ans)
    LR = [(y,x,1) for x, y in R] + [(y,x,-1) for x, y in L]
    get_collision(LR, ans)
    RU = [(x+y,x-y,1) for x, y in R] + [(x+y,x-y,-1) for x,y in U]
    get_collision(RU, ans)
    LD = [(x+y,x-y,1) for x, y in D] + [(x+y,x-y,-1) for x,y in L]
    get_collision(LD,ans)
    LU = [(x-y,x+y,1) for x,y in U] + [(x-y,x+y,-1) for x,y in L]
    get_collision(LU,ans)
    RD = [(x-y,x+y,1) for x,y in R] + [(x-y,x+y,-1) for x,y in D]
    get_collision(RD,ans)
    if ans[0] < 10**10:
        print((ans[0]))
    else:
        print("SAFE")
        
if __name__ == '__main__':
    main()


# Optimized solution
N = int( eval(input()))
XYU = [ tuple( input().split()) for _ in range(N)]
U = []
R = []
D = []
L = []
for x, y, u in XYU:
    x, y = int(x), int(y)
    if u == "U":
        U.append((x,y))
    elif u == "D":
        D.append((x,y))
    elif u == "L":
        L.append((x,y))
    else:
        R.append((x,y))
ans = [10**9]
def z(A):
    A.sort()
    n = -10**10
    p = -10**10
    for k, c, a in A:
        if k != n:
            if a == 1:
                n = k
                p = c
                continue
            else:
                continue
        if a == 1:
            p = c
            continue
        if (c - p)*5 < ans[0]:
            ans[0] = (c - p)*5
z([(x,y,1) for x, y in U] + [(x,y,-1) for x, y in D])
z([(y,x,1) for x, y in R] + [(y,x,-1) for x, y in L])
z([(x+y,x-y,1) for x, y in R] + [(x+y,x-y,-1) for x,y in U])
z([(x+y,x-y,1) for x, y in D] + [(x+y,x-y,-1) for x,y in L])
z([(x-y,x+y,1) for x,y in U] + [(x-y,x+y,-1) for x,y in L])
z([(x-y,x+y,1) for x,y in R] + [(x-y,x+y,-1) for x,y in D])
if ans[0] < 10**9:
    print((ans[0]))
else:
    print("SAFE")
# Original solution
N = int( eval(input()))
XYU = [ tuple( input().split()) for _ in range(N)]
U = []
R = []
D = []
L = []
for x, y, u in XYU:
    x, y = int(x), int(y)
    if u == "U":
        U.append((x,y))
    elif u == "D":
        D.append((x,y))
    elif u == "L":
        L.append((x,y))
    else:
        R.append((x,y))
ans = [10**9]
def z(A):
    A.sort()
    n = -10**10
    p = -10**10
    for k, c, a in A:
        if k != n:
            if a == 1:
                n = k
                p = c
                continue
            else:
                continue
        if a == 1:
            p = c
            continue
        if (c - p)*5 < ans[0]:
            ans[0] = (c - p)*5
z([(x,y,1) for x, y in U] + [(x,y,-1) for x, y in D])
z([(y,x,1) for x, y in R] + [(y,x,-1) for x, y in L])
z([(x+y,x-y,1) for x, y in R] + [(x+y,x-y,-1) for x,y in U])
z([(x+y,x-y,1) for x, y in D] + [(x+y,x-y,-1) for x,y in L])
z([(x-y,x+y,1) for x,y in U] + [(x-y,x+y,-1) for x,y in L])
z([(x-y,x+y,1) for x,y in R] + [(x-y,x+y,-1) for x,y in D])
print(("SAFE" if ans[0] == 10**9 else ans[0]))


# Optimized solution
N=int(eval(input()))
XYU=[tuple(input().split()) for _ in range(N)]
U=[]
R=[]
D=[]
L=[]
for x,y,u in XYU:
    x,y = int(x),int(y)
    if u=="U":
        U.append((x,y))
    elif u=="D":
        D.append((x,y))
    elif u=="L":
        L.append((x,y))
    else:
        R.append((x,y))
ans=[10**9]
def z(A):
    A.sort()
    n=p=-10**9
    for k,c,a in A:
        if k!=n:
            if a==1:
                n=k
                p=c
            continue
        if a==1:
            p=c
            continue
        if (c-p)*5<ans[0]:
            ans[0]=(c-p)*5
z([(x,y,1) for x, y in U]+[(x,y,-1) for x,y in D])
z([(y,x,1) for x, y in R]+[(y,x,-1) for x,y in L])
z([(x+y,x-y,1) for x, y in R]+[(x+y,x-y,-1) for x,y in U])
z([(x+y,x-y,1) for x, y in D]+[(x+y,x-y,-1) for x,y in L])
z([(x-y,x+y,1) for x,y in U]+[(x-y,x+y,-1) for x,y in L])
z([(x-y,x+y,1) for x,y in R]+[(x-y,x+y,-1) for x,y in D])
print(("SAFE" if ans[0]>=10**9 else ans[0]))
# Original solution
N=int(eval(input()))
XYU=[tuple(input().split()) for _ in range(N)]
U=[]
R=[]
D=[]
L=[]
for x,y,u in XYU:
 x,y = int(x),int(y)
 if u=="U":
  U.append((x,y))
 elif u=="D":
  D.append((x,y))
 elif u=="L":
  L.append((x,y))
 else:
  R.append((x,y))
ans=[10**9]
def z(A):
 A.sort()
 n=p=-10**9
 for k,c,a in A:
  if k!=n:
   if a==1:
    n=k
    p=c
   continue
  if a==1:
   p=c
   continue
  if (c-p)*5<ans[0]:
   ans[0]=(c-p)*5
z([(x,y,1) for x,y in U]+[(x,y,-1) for x,y in D])
z([(y,x,1) for x,y in R]+[(y,x,-1) for x,y in L])
z([(x+y,x-y,1) for x,y in R]+[(x+y,x-y,-1) for x,y in U])
z([(x+y,x-y,1) for x,y in D]+[(x+y,x-y,-1) for x,y in L])
z([(x-y,x+y,1) for x,y in U]+[(x-y,x+y,-1) for x,y in L])
z([(x-y,x+y,1) for x,y in R]+[(x-y,x+y,-1) for x,y in D])
print(("SAFE" if ans[0]>=10**9 else ans[0]))


# Optimized solution
N=int(eval(input()))
XYU=[tuple(input().split()) for _ in range(N)]
U=[]
R=[]
D=[]
L=[]
for x,y,u in XYU:
 x,y = int(x),int(y)
 if u=="U":
  U.append((x,y))
 elif u=="D":
  D.append((x,y))
 elif u=="L":
  L.append((x,y))
 else:
  R.append((x,y))
ans=10**9
def z(A):
 global ans
 A.sort()
 n=p=-10**9
 for k,c,a in A:
  if k!=n:
   if a==1:
    n=k
    p=c
   continue
  if a==1:
   p=c
   continue
  if (c-p)*5<ans:
   ans=(c-p)*5
z([(x,y,1) for x,y in U]+[(x,y,-1) for x,y in D])
z([(y,x,1) for x,y in R]+[(y,x,-1) for x,y in L])
z([(x+y,x-y,1) for x,y in R]+[(x+y,x-y,-1) for x,y in U])
z([(x+y,x-y,1) for x,y in D]+[(x+y,x-y,-1) for x,y in L])
z([(x-y,x+y,1) for x,y in U]+[(x-y,x+y,-1) for x,y in L])
z([(x-y,x+y,1) for x,y in R]+[(x-y,x+y,-1) for x,y in D])
print(("SAFE" if ans>=10**9 else ans))
# Original solution
_, *a = list(map(str.split, open(0)))
U, R, D, L = "URDL"
INF = float("inf")
d = {}
for *z, u in a:
  d.setdefault(u, []).append([*list(map(int, z))])
N = INF
for A, B, S in [(U, D, "x, y"), (R, L, "y, x"), 
                (R, U, "x+y, x-y"), (D, L, "x+y, x-y"), 
                (U, L, "x-y, x+y"), (R, D, "x-y, x+y")]:
  n = p = -INF
  L = eval(f"[({S}, Q) for P, Q in [(A, 1), (B, 0)] for x, y in d.get(P, [])]")
  for x, y, r in sorted(L):
    if x != n:
      if r:n = x;p = y
    elif r:p = y
    else:N = min(N, (y - p)*5)
print(("SAFE" if N == INF else N))

# Optimized solution
_,*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)*5)
print((["SAFE",N][N!=9e9]))
# Original solution
_,*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]))

# Optimized solution
U,R,D,L="URDL"
d={i:[]for i in"URDL"}
_,*a=list(map(str.split,open(0)))
for*z,u in a:d[u]+=[*list(map(int,z))],
N,P,M=9e9,"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]))
# Original solution
U,R,D,L="URDL"
d={i:[]for i in"URDL"}
_,*a=list(map(str.split,open(0)))
for*z,u in a:d[u]+=[*list(map(int,z))],
N,P,M=9e9,"x+y,","x-y,"
for*F,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=-9e9
 for x,y,r in sorted(eval(f"[({S}Q)for Q,P in enumerate(F)for x,y in d[P]]")):
  if r:n,p=x,y
  elif x==n:N=min(N,y-p)
print((["SAFE",N*5][N<9e9]))

# Optimized solution
U,R,D,L="URDL"
d={i:[]for i in"URDL"}
_,*a=list(map(str.split,open(0)))
for*z,u in a:d[u]+=[*list(map(int,z))],
I=N=9e9;P,M="x+y,","x-y,"
for*F,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 sorted(eval(f"[({S}Q)for Q,P in enumerate(F)for x,y in d[P]]")):
  if r:n,p=x,y
  elif x==n:N=min(N,y-p)
print((["SAFE",N*5][N<I]))
# Original solution
U,R,D,L="URDL"
d={i:[]for i in"URDL"}
_,*a=list(map(str.split,open(0)))
for*z,u in a:d[u]+=[*list(map(int,z))],
I=N=9e9;P,M="x+y,","x-y,"
for*F,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 sorted(eval(f"[({S}Q)for Q,P in enumerate(F)for x,y in d[P]]")):
  if r:n,p=x,y
  elif x==n:N=min(N,y-p)
print((["SAFE",N*5][N<I]))

# Optimized solution
s="URDL";d={i:[]for i in s}
_,*a=list(map(str.split,open(0)))
for*z,u in a:d[u]+=[*list(map(int,z))],
I=N=9e9;P,M="x+y,","x-y,"
for*F,S in(2,0,"x,y,"),(3,1,"y,x,"),(0,1,P+M),(3,2,P+M),(3,0,M+P),(2,1,M+P):
 n=p=-I
 for x,y,r in sorted(eval(f"(({S}Q)for Q,P in enumerate(F)for x,y in d[s[P]])")):
  if r:n,p=x,y
  elif x==n:N=min(N,y-p)
print((["SAFE",N*5][N<I]))
# Original solution
import sys
input = lambda: sys.stdin.readline().rstrip('\r\n')

from collections import defaultdict

N = int(eval(input()))

S = 'RULD'

class Plane:
	__slots__ = ('x', 'y', 'd')

	def __init__(s, x, y, d):
		s.x, s.y, s.d = int(x), int(y), d

	def __lt__(s, o):
		return s.x < o.x

	def rotate(s):
		s.x, s.y = -s.y, s.x
		s.d = S[(S.index(s.d)+1)%4]


P = [Plane(*input().split()) for _ in range(N)]

INF = 10 ** 18
best = INF
MAXC = 400000

for trial in range(4):
	P.sort()

	rdu, rdd, ry = ([INF] * (2*MAXC+1) for _ in range(3))

	for p in P:
		if p.d == 'R':
			ry[p.y+MAXC] = p.x
			rdu[p.y-p.x+MAXC] = p.x
			rdd[p.y+p.x+MAXC] = p.x
		elif p.d == 'L':
			ty = p.y + MAXC
			if ry[ty] != INF:
				best = min(best, (p.x - ry[ty]) * 5)
		elif p.d == 'U':
			ty = p.x + p.y + MAXC
			if rdd[ty] != INF:
				best = min(best, (p.x - rdd[ty]) * 10)
		elif p.d == 'D':
			ty = p.y - p.x + MAXC
			if rdu[ty] != INF:
				best = min(best, (p.x - rdu[ty]) * 10)

	if trial != 3:
		for p in P: p.rotate()

print(('SAFE' if best == INF else best))


# Optimized solution
import sys
input = lambda: sys.stdin.readline().rstrip('\r\n')

from collections import defaultdict

N = int(eval(input()))

S = 'RULD'

class Plane:
	__slots__ = ('x', 'y', 'd')

	def __init__(s, x, y, d):
		s.x, s.y, s.d = int(x), int(y), d

	def __lt__(s, o):
		return s.x < o.x

	def rotate(s):
		s.x, s.y = -s.y, s.x
		s.d = S[(S.index(s.d)+1)%4]

P = []
for _ in range(N):
	x, y, d = input().split()
	P.append((int(x), int(y), d))

def rotate(s):
	return (-s[1], s[0], S[(S.index(s[2])+1)%4])

INF = 10 ** 18
best = INF
MAXC = 400000

for trial in range(4):
	P.sort(key=lambda v: v[0])

	rdu, rdd, ry = ([INF] * (2*MAXC+1) for _ in range(3))

	for (x, y, d) in P:
		if d == 'R':
			ry[y+MAXC] = x
			rdu[y-x+MAXC] = x
			rdd[y+x+MAXC] = x
		elif d == 'L':
			ty = y + MAXC
			if ry[ty] != INF:
				best = min(best, (x - ry[ty]) * 5)
		elif d == 'U':
			ty = x + y + MAXC
			if rdd[ty] != INF:
				best = min(best, (x - rdd[ty]) * 10)
		elif d == 'D':
			ty = y - x + MAXC
			if rdu[ty] != INF:
				best = min(best, (x - rdu[ty]) * 10)

	if trial != 3:
		P = [rotate(p) for p in P]

print(('SAFE' if best == INF else best))
# Original solution
from collections import defaultdict
from bisect import *

def into_dict(dics,x,y,u):
  x,y = int(x),int(y)
  insort(dics[u+'_x'][x],y)
  insort(dics[u+'_y'][y],x)
  insort(dics[u+'_xpy'][x+y],x)
  insort(dics[u+'_ymx'][y-x],x)
  return dics

def para(dics,dic1,dic2):
  ans = float('inf')
  for k,v in list(dics[dic1].items()):
    for zahyou in v[1:-1]:
      ind = bisect_right(dics[dic2][k],zahyou)
      dis = zahyou-dics[dic2][k][ind-1]
      ans = min(ans,dis)
  return ans

def solve():
  ans = []
  N = int(eval(input()))
  dics = defaultdict(lambda: defaultdict(lambda: [-float('inf'),float('inf')]))
  for i in range(N):
    dics = into_dict(dics,*input().split())
  lis1 = [['L_y','R_y'],['D_x','U_x']]
  lis2 = [['L_ymx','U_ymx'],['U_xpy','R_xpy'],['L_xpy','D_xpy'],['D_ymx','R_ymx']]
  for arg in lis1:
    ans.append(para(dics,*arg)*5)
  for arg in lis2:
    ans.append(para(dics,*arg)*10)
  ans = min(ans)
  return ans if ans<float('inf') else 'SAFE'
print((solve()))

# Optimized solution
from collections import defaultdict
from bisect import *

def into_dict(dics,x,y,u):
  x,y = int(x),int(y)
  insort(dics[u+'_x'][x],y)
  insort(dics[u+'_y'][y],x)
  insort(dics[u+'_xpy'][x+y],x)
  insort(dics[u+'_ymx'][y-x],x)
  return dics

def para(dics,dic1,dic2):
  ans = float('inf')
  for k,v in list(dics[dic1].items()):
    for zahyou in v[1:-1]:
      ind = bisect_right(dics[dic2][k],zahyou)
      dis = zahyou-dics[dic2][k][ind-1]
      ans = min(ans,dis)
  return ans

import sys; input=sys.stdin.readline
def solve():
  ans = []
  N = int(eval(input()))
  dics = defaultdict(lambda: defaultdict(lambda: [-float('inf'),float('inf')]))
  for i in range(N):
    dics = into_dict(dics,*input().split())
  lis1 = [['L_y','R_y'],['D_x','U_x']]
  lis2 = [['L_ymx','U_ymx'],['U_xpy','R_xpy'],['L_xpy','D_xpy'],['D_ymx','R_ymx']]
  for arg in lis1:
    ans.append(para(dics,*arg)*5)
  for arg in lis2:
    ans.append(para(dics,*arg)*10)
  ans = min(ans)
  return ans if ans<float('inf') else 'SAFE'
print((solve()))
# Original solution
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'))

# Optimized solution
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'))
# Original solution
I,*U=N,*D,=9**9,
P,*L='x+y,',
M,*R='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=I
  for x,y,r in eval('sorted((%sQ)for Q,P in enumerate(A)for x,y in P)'%S):exec(r*'n,p=x,y'or'N=min(N,y-p)'*(x==n))
print((N%I*5or'SAFE'))

# Optimized solution
I,*U=N,*D=9**9,
P,*L='x+y,',
M,*R='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):
 for x,y,r in eval('sorted((%sQ)for Q,P in enumerate(A)for x,y in P)'%S):
  if r:S,p=x,y
  elif x==S:N=min(N,y-p)
print((N%I*5or'SAFE'))
# Original solution
# coding: utf-8
# Your code here!
import sys
read = sys.stdin.read
readline = sys.stdin.readline

n, = list(map(int, readline().split()))

M = 2*10**5+2
Uy = [[] for _ in range(M)]
Dy = [[] for _ in range(M)]

Rx = [[] for _ in range(M)]
Lx = [[] for _ in range(M)]

d1U = {} # 右上、x+y UR
d1R = {} # 右上、x+y UR
d2U = {} # 左上、x-y UL
d2L = {} # 左上、x-y UL
d3L = {} # 左下、x+y LD
d3D = {} # 左下、x+y LD
d4D = {} # 右下、x-y DR
d4R = {} # 右下、x-y DR

for _ in range(n):
    x,y,z = readline().split()
    x = int(x); y = int(y)
    if z=="U":
        Uy[x].append(y)
        dp,dn = d1U,d2U
    elif z=="D":
        Dy[x].append(y)
        dp,dn = d3D,d4D
    elif z=="R":
        Rx[y].append(x)
        dp,dn = d1R,d4R
    elif z=="L":
        Lx[y].append(x)
        dp,dn = d3L,d2L
    
    v = x+y
    if v in dp: dp[v].append(x)
    else: dp[v] = [x]
    v = x-y
    if v in dn: dn[v].append(x)
    else: dn[v] = [x]

def get(a,b):
    v = INF
    lb = len(b)
    j = 0
    for ai in a:
        while j < lb and b[j] < ai:
            j += 1
        if j < lb:
            v = min(v,b[j]-ai)
    return v

def syoumen(lst1,lst2):
    v = INF
    for i in range(M):
        if lst1[i] and lst2[i]:
            lst1[i].sort()
            lst2[i].sort()
            v = min(v,get(lst1[i],lst2[i]))
    return v*5

def naname(d1,d2):
    v = INF
    for i in range(-2*M,2*M):
        if i in d1 and i in d2:
            d1[i].sort()
            d2[i].sort()
            v = min(v,get(d1[i],d2[i]))
    return v*10

INF = 1<<30
ans = INF

ans = min(ans,syoumen(Uy,Dy))
ans = min(ans,syoumen(Rx,Lx))

ans = min(ans,naname(d1R,d1U))
ans = min(ans,naname(d2U,d2L))
ans = min(ans,naname(d3D,d3L))
ans = min(ans,naname(d4R,d4D))

if ans == INF:
    print("SAFE")
else:
    print(ans)



# Optimized solution
# coding: utf-8
# Your code here!
import sys
read = sys.stdin.read
readline = sys.stdin.readline

n, = list(map(int, readline().split()))

M = 2*10**5+2
Uy = {}
Dy = {}
Rx = {}
Lx = {}

d1U = {} # 右上、x+y UR
d1R = {} # 右上、x+y UR
d2U = {} # 左上、x-y UL
d2L = {} # 左上、x-y UL
d3L = {} # 左下、x+y LD
d3D = {} # 左下、x+y LD
d4D = {} # 右下、x-y DR
d4R = {} # 右下、x-y DR

for _ in range(n):
    x,y,z = readline().split()
    x = int(x); y = int(y)
    if z=="U":
        ss,dp,dn = Uy,d1U,d2U
    elif z=="D":
        ss,dp,dn = Dy,d3D,d4D
    elif z=="R":
        ss,dp,dn = Rx,d1R,d4R
    elif z=="L":
        ss,dp,dn = Lx,d3L,d2L
    
    v = x+y
    if v in dp: dp[v].append(x)
    else: dp[v] = [x]

    v = x-y
    if v in dn: dn[v].append(x)
    else: dn[v] = [x]
    
    if z=="U" or z=="D": x,y = y,x
    if y in ss: ss[y].append(x)
    else: ss[y] = [x]
    
def get(a,b,r):
    a.sort()
    b.sort()
    v = INF
    lb = len(b)
    j = 0
    for ai in a:
        while j < lb and b[j] < ai:
            j += 1
        if j < lb:
            v = min(v,b[j]-ai)
    return v*r

INF = 1<<30
ans = INF

for D1,D2,r in [(Uy,Dy,5),(Rx,Lx,5),(d1R,d1U,10),(d2U,d2L,10),(d3D,d3L,10),(d4R,d4D,10)]:
    for k,lst in list(D1.items()):
        if k in D2:
            ans = min(ans,get(lst,D2[k],r))

if ans == INF:
    print("SAFE")
else:
    print(ans)
# Original solution
n,m=[ord(i) for i in input().split()];n-=m;print((['<','>'][n>0] if n else'='))

# Optimized solution
n,m=input().split();print((['=',['<','>'][n>m]][n!=m]))
# Original solution
x, y = input().split()
if x < y:
    print("<")
elif x == y:
    print("=")
else:
    print(">")

# Optimized solution
x, y = input().split()
print(("<" if x < y else "=" if x == y else ">"))
# Original solution
X, Y =input().split()
X = int(X, 16)
Y = int(Y, 16)

if(X<Y):
    print('<')

elif(X>Y):
    print('>')

else:
    print('=')

# Optimized solution
X, Y =input().split()
X = int(X, 16)
Y = int(Y, 16)
print((['=', '<', '>'][(X<Y) - (Y<X)]))
# Original solution
def tonum(a):
    if a == 'A':
        return 10
    elif a == 'B':
        return 11
    elif a == 'C':
        return 12
    elif a == 'D':
        return 13
    elif a == 'E':
        return 14
    elif a == 'F':
        return 15
    else:
        return int(a)

a, b = input().split()
a = tonum(a)
b = tonum(b)
if a < b:
    print('<')
elif a > b:
    print('>')
else:
    print('=')

# Optimized solution
a, b = list(map(str, input().split()))
if a < b:
    print('<')
elif a > b:
    print('>')
else:
    print('=')
# Original solution
from operator import sub; print(('<<<<<=>>>>>'[sub(*[int(s, 16) for s in (input().split())])+5]))


# Optimized solution
print(('<<<<<=>>>>>'[(lambda a,b:a-b)(*list(map(ord,(input().split()))))+5]))
# Original solution
X, Y = list(map(str, input().split()))
if X == 'A':
    x = 10
elif X == 'B':
    x = 11
elif X == 'C':
    x = 12
elif X == 'D':
    x = 13
elif X == 'E':
    x = 14
elif X == 'F':
    x = 15

if Y == 'A':
    y = 10
elif Y == 'B':
    y = 11
elif Y == 'C':
    y = 12
elif Y == 'D':
    y = 13
elif Y == 'E':
    y = 14
elif Y == 'F':
    y = 15

if x < y:
    print('<')
elif x == y:
    print('=')
else:
    print('>')


# Optimized solution
A, B = input().split()
if A > B: 
    print('>')
elif A == B:
    print('=')
else:
    print('<')
# Original solution
# -*- coding: utf-8 -*-


def main():
    dict = {'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15}
    x, y = input().split()
    if dict[x] > dict[y]:
        print('>')
    elif dict[x] < dict[y]:
        print('<')
    else:
        print('=')


if __name__ == '__main__':
    main()

# Optimized solution
# -*- coding: utf-8 -*-

dic = {'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15}


def func(x, y):

    if dic[x] > dic[y]:
        result = '>'
    elif dic[x] < dic[y]:
        result = '<'
    else:
        result = '='
    return result


X, Y = input().split()
print((func(X, Y)))
# Original solution
a,b=input().split();print(([[">","="][ord(a)==ord(b)],"<"][ord(a)<ord(b)]))

# Optimized solution
x,y=input().split();print(("=><"[(x>y)-(x<y)]))
# Original solution
X,Y = input().split()
c = {"A":11,"B":12,"C":13,"D":14,"E":15,"F":16}
if c[X]== c[Y]:
    print("=")
elif c[X] > c[Y]:
    print(">")
else:
    print("<")

# Optimized solution
X,Y = input().split()
if X > Y:
    print(">")
elif X < Y:
    print("<")
else:
    print("=")
# Original solution
a,b=input().split();print((['=','<','>'][(a!=b)+(a>b)]))

# Optimized solution
a,b=input().split();print(("=<>"[(a!=b)+(a>b)]))