Buggy python submission (verdict: Time Limit Exceeded):
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 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(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())

Fixed submission:
from collections import defaultdict
from bisect import *
def solve():
  ans = float('inf')
  N = int(input())
  L_y = defaultdict(lambda: [-float('inf'),float('inf')])
  R_y = defaultdict(lambda: [-float('inf'),float('inf')])
  U_x = defaultdict(lambda: [-float('inf'),float('inf')])
  D_x = defaultdict(lambda: [-float('inf'),float('inf')])
  R_xpy = defaultdict(lambda: [-float('inf'),float('inf')])
  R_y_x = defaultdict(lambda: [-float('inf'),float('inf')])
  L_xpy = defaultdict(lambda: [-float('inf'),float('inf')])
  L_y_x = defaultdict(lambda: [-float('inf'),float('inf')])
  U_xpy = defaultdict(lambda: [-float('inf'),float('inf')])
  U_y_x = defaultdict(lambda: [-float('inf'),float('inf')])
  D_xpy = defaultdict(lambda: [-float('inf'),float('inf')])
  D_y_x = defaultdict(lambda: [-float('inf'),float('inf')])
  for i in range(N):
    x,y,u = input().split()
    x,y = int(x),int(y)
    if u=='D':
      insort(D_x[x],y)
      insort(D_xpy[x+y],x)
      insort(D_y_x[y-x],x)
    if u=='U':
      insort(U_x[x],y)
      insort(U_xpy[x+y],x)
      insort(U_y_x[y-x],x)
    if u=='L':
      insort(L_y[y],x)
      insort(L_xpy[x+y],x)
      insort(L_y_x[y-x],x)
    if u=='R':
      insort(R_y[y],x)
      insort(R_xpy[x+y],x)
      insort(R_y_x[y-x],x)
  for k,v in R_y.items():
    for zahyou in v[1:-1]:
      ind = bisect_right(L_y[k],zahyou)
      dis = L_y[k][ind]-zahyou
      ans = min(ans,dis)
  for k,v in D_x.items():
    for zahyou in v[1:-1]:
      ind = bisect_right(U_x[k],zahyou)
      dis = zahyou-U_x[k][ind-1]
      ans = min(ans,dis)
  for k,v in L_y_x.items():
    for zahyou in v[1:-1]:
      ind = bisect_right(U_y_x[k],zahyou)
      dis = zahyou-U_y_x[k][ind-1]
      dis *= 2
      ans = min(ans,dis)
  for k,v in R_xpy.items():
    for zahyou in v[1:-1]:
      ind = bisect_right(U_xpy[k],zahyou)
      dis = U_xpy[k][ind]-zahyou
      dis *= 2
      ans = min(ans,dis)
  for k,v in L_xpy.items():
    for zahyou in v[1:-1]:
      ind = bisect_right(D_xpy[k],zahyou)
      dis = zahyou-D_xpy[k][ind-1]
      dis *= 2
      ans = min(ans,dis)
  for k,v in R_y_x.items():
    for zahyou in v[1:-1]:
      ind = bisect_right(D_y_x[k],zahyou)
      dis = D_y_x[k][ind]-zahyou
      dis *= 2
      ans = min(ans,dis)
  ans *= 5
  return ans if ans<float('inf') else 'SAFE'
print(solve())

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
from bisect import *
def solve():
  ans = float('inf')
  N = int(input())
  L_y = defaultdict(lambda: [-float('inf'),float('inf')])
  R_y = defaultdict(lambda: [-float('inf'),float('inf')])
  U_x = defaultdict(lambda: [-float('inf'),float('inf')])
  D_x = defaultdict(lambda: [-float('inf'),float('inf')])
  R_xpy = defaultdict(lambda: [-float('inf'),float('inf')])
  R_y_x = defaultdict(lambda: [-float('inf'),float('inf')])
  L_xpy = defaultdict(lambda: [-float('inf'),float('inf')])
  L_y_x = defaultdict(lambda: [-float('inf'),float('inf')])
  U_xpy = defaultdict(lambda: [-float('inf'),float('inf')])
  U_y_x = defaultdict(lambda: [-float('inf'),float('inf')])
  D_xpy = defaultdict(lambda: [-float('inf'),float('inf')])
  D_y_x = defaultdict(lambda: [-float('inf'),float('inf')])
  for i in range(N):
    x,y,u = input().split()
    x,y = int(x),int(y)
    if u=='D':
      insort(D_x[x],y)
      insort(D_xpy[x+y],x)
      insort(D_y_x[y-x],x)
    if u=='U':
      insort(U_x[x],y)
      insort(U_xpy[x+y],x)
      insort(U_y_x[y-x],x)
    if u=='L':
      insort(L_y[y],x)
      insort(L_xpy[x+y],x)
      insort(L_y_x[y-x],x)
    if u=='R':
      insort(R_y[y],x)
      insort(R_xpy[x+y],x)
      insort(R_y_x[y-x],x)
  for k,v in R_y.items():
    for zahyou in v[1:-1]:
      ind = bisect_right(L_y[k],zahyou)
      dis = L_y[k][ind]-zahyou
      ans = min(ans,dis)
  for k,v in D_x.items():
    for zahyou in v[1:-1]:
      ind = bisect_right(U_x[k],zahyou)
      dis = zahyou-U_x[k][ind-1]
      ans = min(ans,dis)
  for k,v in L_y_x.items():
    for zahyou in v[1:-1]:
      ind = bisect_right(U_y_x[k],zahyou)
      dis = zahyou-U_y_x[k][ind-1]
      dis *= 2
      ans = min(ans,dis)
  for k,v in R_xpy.items():
    for zahyou in v[1:-1]:
      ind = bisect_right(U_xpy[k],zahyou)
      dis = U_xpy[k][ind]-zahyou
      dis *= 2
      ans = min(ans,dis)
  for k,v in L_xpy.items():
    for zahyou in v[1:-1]:
      ind = bisect_right(D_xpy[k],zahyou)
      dis = zahyou-D_xpy[k][ind-1]
      dis *= 2
      ans = min(ans,dis)
  for k,v in R_y_x.items():
    for zahyou in v[1:-1]:
      ind = bisect_right(D_y_x[k],zahyou)
      dis = D_y_x[k][ind]-zahyou
      dis *= 2
      ans = min(ans,dis)
  ans *= 5
  return ans if ans<float('inf') else 'SAFE'
print(solve())

Fixed submission:
from collections import defaultdict
from bisect import *
def solve():
  ans = float('inf')
  N = int(input())
  L_y = defaultdict(lambda: [-float('inf'),float('inf')])
  R_y = defaultdict(lambda: [-float('inf'),float('inf')])
  U_x = defaultdict(lambda: [-float('inf'),float('inf')])
  D_x = defaultdict(lambda: [-float('inf'),float('inf')])
  R_xpy = defaultdict(lambda: [-float('inf'),float('inf')])
  R_y_x = defaultdict(lambda: [-float('inf'),float('inf')])
  L_xpy = defaultdict(lambda: [-float('inf'),float('inf')])
  L_y_x = defaultdict(lambda: [-float('inf'),float('inf')])
  U_xpy = defaultdict(lambda: [-float('inf'),float('inf')])
  U_y_x = defaultdict(lambda: [-float('inf'),float('inf')])
  D_xpy = defaultdict(lambda: [-float('inf'),float('inf')])
  D_y_x = defaultdict(lambda: [-float('inf'),float('inf')])
  for i in range(N):
    x,y,u = input().split()
    x,y = int(x),int(y)
    if u=='D':
      insort(D_x[x],y)
      insort(D_xpy[x+y],x)
      insort(D_y_x[y-x],x)
    if u=='U':
      insort(U_x[x],y)
      insort(U_xpy[x+y],x)
      insort(U_y_x[y-x],x)
    if u=='L':
      insort(L_y[y],x)
      insort(L_xpy[x+y],x)
      insort(L_y_x[y-x],x)
    if u=='R':
      insort(R_y[y],x)
      insort(R_xpy[x+y],x)
      insort(R_y_x[y-x],x)
  for k,v in R_y.items():
    for zahyou in v[1:-1]:
      ind = bisect_right(L_y[k],zahyou)
      dis = L_y[k][ind]-zahyou
      ans = min(ans,dis)
  for k,v in D_x.items():
    for zahyou in v[1:-1]:
      ind = bisect_right(U_x[k],zahyou)
      dis = zahyou-U_x[k][ind-1]
      ans = min(ans,dis)
  for k,v in L_y_x.items():
    for zahyou in v[1:-1]:
      ind = bisect_right(U_y_x[k],zahyou)
      dis = zahyou-U_y_x[k][ind-1]
      dis *= 2
      ans = min(ans,dis)
  for k,v in R_xpy.items():
    for zahyou in v[1:-1]:
      ind = bisect_right(U_xpy[k],zahyou)
      dis = U_xpy[k][ind]-zahyou
      dis *= 2
      ans = min(ans,dis)
  for k,v in L_xpy.items():
    for zahyou in v[1:-1]:
      ind = bisect_right(D_xpy[k],zahyou)
      dis = zahyou-D_xpy[k][ind-1]
      dis *= 2
      ans = min(ans,dis)
  for k,v in R_y_x.items():
    for zahyou in v[1:-1]:
      ind = bisect_right(D_y_x[k],zahyou)
      dis = D_y_x[k][ind]-zahyou
      dis *= 2
      ans = min(ans,dis)
  ans *= 5
  return ans if ans<float('inf') else 'SAFE'
print(solve())