n=int(input())
ver=[]
hor=[]
diagr=[]
diagl=[]
INF=10**9
for i in range(200001):
  ver.append([[],[]])
  hor.append([[],[]])
for i in range(-200000,200001):
  diagr.append([[],[],[],[]])
  diagl.append([[],[],[],[]])
for i in range(n):
  x,y,u=map(str,input().split())
  x=int(x)
  y=int(y)
  if u=='U':
    ver[x][0].append(y)
    diagr[x-y+200000][0].append(x)
    diagl[x+y][0].append(x)
  if u=='D':
    ver[x][1].append(y)
    diagr[x-y+200000][1].append(x)
    diagl[x+y][1].append(x)
  if u=='R':
    hor[y][0].append(x)
    diagr[x-y+200000][2].append(x)
    diagl[x+y][2].append(x)
  if u=='L':
    hor[y][1].append(x)
    diagr[x-y+200000][3].append(x)
    diagl[x+y][3].append(x)
ans=INF
for i in range(200001):
  if len(ver[i][0])>0 and len(ver[i][1])>0:
    ver[i][0].sort()
    ver[i][1].sort()
    if ver[i][0][0]>ver[i][1][-1]:
      continue
    pointer1=0
    pointer=0
    s=len(ver[i][1])
    t=len(ver[i][0])
    while pointer1<t:
      while pointer<s and pointer1<t and ver[i][1][pointer]<ver[i][0][pointer1]:
        pointer+=1
      if pointer==s:
        break
      ans=min(ans,(ver[i][1][pointer]-ver[i][0][pointer1])*5)
      pointer1+=1
for i in range(200001):
  if len(hor[i][0])>0 and len(hor[i][1])>0:
    hor[i][0].sort()
    hor[i][1].sort()
    if hor[i][0][0]>hor[i][1][-1]:
      continue
    pointer1=0
    pointer=0
    s=len(hor[i][1])
    t=len(hor[i][0])
    while pointer1<t:
      while pointer<s and pointer1<t and hor[i][1][pointer]<hor[i][0][pointer1]:
        pointer+=1
      if pointer==s:
        break
      ans=min(ans,(hor[i][1][pointer]-hor[i][0][pointer1])*5)
      pointer1+=1
for i in range(400001):
  if len(diagl[i][0])>0 and len(diagl[i][2])>0:
    diagl[i][0].sort()
    diagl[i][2].sort()
    if diagl[i][2][0]>diagl[i][0][-1]:
      continue
    pointer1=0
    pointer=0
    s=len(diagl[i][0])
    t=len(diagl[i][2])
    while pointer1<t:
      while pointer<s and pointer1<t and diagl[i][0][pointer]<diagl[i][2][pointer1]:
        pointer+=1
      if pointer==s:
        break
      ans=min(ans,(diagl[i][0][pointer]-diagl[i][2][pointer1])*10)
      pointer1+=1
  if len(diagl[i][1])>0 and len(diagl[i][3])>0:
    diagl[i][1].sort()
    diagl[i][3].sort()
    if diagl[i][1][0]>diagl[i][3][-1]:
      continue
    pointer1=0
    pointer=0
    s=len(diagl[i][3])
    t=len(diagl[i][1])
    while pointer1<t:
      while pointer<s and pointer1<t and diagl[i][3][pointer]<diagl[i][1][pointer1]:
        pointer+=1
      if pointer==s:
        break
      ans=min(ans,(diagl[i][3][pointer]-diagl[i][1][pointer1])*10)
      pointer1+=1
for i in range(400001):
  if len(diagr[i][0])>0 and len(diagr[i][3])>0:
    diagr[i][0].sort()
    diagr[i][3].sort()
    if diagr[i][0][0]>diagr[i][3][-1]:
      continue
    pointer1=0
    pointer=0
    s=len(diagr[i][3])
    t=len(diagr[i][0])
    while pointer1<t:
      while pointer<s and pointer1<t and diagr[i][3][pointer]<diagr[i][0][pointer1]:
        pointer+=1
      if pointer==s:
        break
      ans=min(ans,(diagr[i][3][pointer]-diagr[i][0][pointer1])*10)
      pointer1+=1
  if len(diagr[i][1])>0 and len(diagr[i][2])>0:
    diagr[i][1].sort()
    diagr[i][2].sort()
    if diagr[i][2][0]>diagr[i][1][-1]:
      continue
    pointer1=0
    pointer=0
    s=len(diagr[i][1])
    t=len(diagr[i][2])
    while pointer1<t:
      while pointer<s and pointer1<t and diagr[i][1][pointer]<diagr[i][2][pointer1]:
        pointer+=1
      if pointer==s:
        break
      ans=min(ans,(diagr[i][1][pointer]-diagr[i][2][pointer1])*10)
      pointer1+=1
if ans==INF:
  print('SAFE')
else:
  print(ans)
      

Measured memory_bytes: 337072