Buggy python submission (verdict: Runtime Error):
from collections import defaultdict
from collections import deque
from collections import Counter
import itertools
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n = readInt()
xyu = [readChars() for i in range(n)]

class mydict:
	def __init__(self):
		self.d = {}
	def add(self,key,value):
		if key in self.d:
			self.d[key].append(value)
		else:
			self.d[key]=[value]
	def p(self):
		print("{")
		for key in self.d:
			print(key,self.d[key])
		print("}")
	def mysort(self):
		for i in self.d:
			self.d[i].sort()
	def getDict(self):
		return self.d;

ud = mydict()
rl = mydict()
for x,y,u in xyu:
	if u=="U" or u=="D":
		ud.add(int(x),[int(y),u])
	else:
		rl.add(int(y),[int(x),u])
ud.mysort()
rl.mysort()
#ud.p()
#rl.p()
ans = float("inf")

tar = ud.getDict()
for key in tar:
	s = ""
	be = "-1"
	bev = "-1"
	for v in tar[key]:
		if be=="U" and v[1]=="D":
			ans = min(ans,(int(v[0])-int(bev))*5)
		be = v[1]
		bev = v[0]
tar = rl.getDict()
for key in tar:
	s = ""
	be = "-1"
	bev = "-1"
	for v in tar[key]:
		if be=="R" and v[1]=="L":
			ans = min(ans,(int(v[0])-int(bev))*5)
			raise Exception
		be = v[1]
		bev = v[0]

xy = mydict()
for x,y,u in xyu:
	xy.add(int(x)+int(y),[int(x),u])
#xy.p()
xy.mysort()
d = xy.getDict()
for key in d:
	beu = "-1"
	bex = -1
	for arr in d[key]:
		if beu=="R" and arr[1]=="U":
			ans = min(ans,(arr[0]-bex)*10)
		if beu=="D" and arr[1]=="L":
			ans = min(ans,(arr[0]-bex)*10)
		beu = arr[1]
		bex = arr[0]
mxy = mydict()
for x,y,u in xyu:
	mxy.add(int(y)-int(x),[int(x),u])
mxy.mysort()
#mxy.p()
d = mxy.getDict()
for key in d:
	beu = "-1"
	bex = -1
	for arr in d[key]:
		if beu=="R" and arr[1]=="D":
			ans = min(ans,(arr[0]-bex)*10)
		if beu=="U" and arr[1]=="L":
			ans = min(ans,(arr[0]-bex)*10)
		beu = arr[1]
		bex = arr[0]

if ans==float("inf"):
	print("SAFE")
else:
	print(ans)


Fixed submission:
from collections import defaultdict
from collections import deque
from collections import Counter
import itertools
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n = readInt()
xyu = [readChars() for i in range(n)]

class mydict:
	def __init__(self):
		self.d = {}
	def add(self,key,value):
		if key in self.d:
			self.d[key].append(value)
		else:
			self.d[key]=[value]
	def p(self):
		print("{")
		for key in self.d:
			print(key,self.d[key])
		print("}")
	def mysort(self):
		for i in self.d:
			self.d[i].sort()
	def getDict(self):
		return self.d;

ud = mydict()
rl = mydict()
for x,y,u in xyu:
	if u=="U" or u=="D":
		ud.add(int(x),[int(y),u])
	else:
		rl.add(int(y),[int(x),u])
ud.mysort()
rl.mysort()
#ud.p()
#rl.p()
ans = float("inf")

tar = ud.getDict()
for key in tar:
	beu = "-1"
	bev = "-1"
	for v in tar[key]:
		if beu=="U" and v[1]=="D":
			ans = min(ans,(int(v[0])-int(bev))*5)
		beu = v[1]
		bev = v[0]
tar = rl.getDict()
for key in tar:
	s = ""
	beu = "-1"
	bev = "-1"
	for v in tar[key]:
		if beu=="R" and v[1]=="L":
			ans = min(ans,(int(v[0])-int(bev))*5)
		beu = v[1]
		bev = v[0]

xy = mydict()
for x,y,u in xyu:
	xy.add(int(x)+int(y),[int(x),u])
#xy.p()
xy.mysort()
d = xy.getDict()
for key in d:
	beu = "-1"
	bex = -1
	for arr in d[key]:
		if beu=="R" and arr[1]=="U":
			ans = min(ans,(arr[0]-bex)*10)
		if beu=="D" and arr[1]=="L":
			ans = min(ans,(arr[0]-bex)*10)
		beu = arr[1]
		bex = arr[0]
mxy = mydict()
for x,y,u in xyu:
	mxy.add(int(y)-int(x),[int(x),u])
mxy.mysort()
#mxy.p()
d = mxy.getDict()
for key in d:
	beu = "-1"
	bex = -1
	for arr in d[key]:
		if beu=="R" and arr[1]=="D":
			ans = min(ans,(arr[0]-bex)*10)
		if beu=="U" and arr[1]=="L":
			ans = min(ans,(arr[0]-bex)*10)
		beu = arr[1]
		bex = arr[0]

if ans==float("inf"):
	print("SAFE")
else:
	print(ans)

Buggy python submission (verdict: Runtime Error):
from collections import defaultdict
from collections import deque
from collections import Counter
import itertools
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n = readInt()
xyu = [readChars() for i in range(n)]

class mydict:
	def __init__(self):
		self.d = {}
	def add(self,key,value):
		if key in self.d:
			self.d[key].append(value)
		else:
			self.d[key]=[value]
	def p(self):
		print("{")
		for key in self.d:
			print(key,self.d[key])
		print("}")
	def mysort(self):
		for i in self.d:
			self.d[i].sort()
	def getDict(self):
		return self.d;

ud = mydict()
rl = mydict()
for x,y,u in xyu:
	if u=="U" or u=="D":
		ud.add(int(x),[int(y),u])
	else:
		rl.add(int(y),[int(x),u])
ud.mysort()
rl.mysort()
#ud.p()
#rl.p()
ans = float("inf")

tar = ud.getDict()
for key in tar:
	s = ""
	be = "-1"
	bev = "-1"
	for v in tar[key]:
		if be=="U" and v[1]=="D":
			ans = min(ans,(int(v[0])-int(bev))*5)
			raise Exception
		be = v[1]
		bev = v[0]
tar = rl.getDict()
for key in tar:
	s = ""
	be = "-1"
	bev = "-1"
	for v in tar[key]:
		if be=="R" and v[1]=="L":
			ans = min(ans,(int(v[0])-int(bev))*5)
		be = v[1]
		bev = v[0]

xy = mydict()
for x,y,u in xyu:
	xy.add(int(x)+int(y),[int(x),u])
#xy.p()
xy.mysort()
d = xy.getDict()
for key in d:
	beu = "-1"
	bex = -1
	for arr in d[key]:
		if beu=="R" and arr[1]=="U":
			ans = min(ans,(arr[0]-bex)*10)
		if beu=="D" and arr[1]=="L":
			ans = min(ans,(arr[0]-bex)*10)
		beu = arr[1]
		bex = arr[0]
mxy = mydict()
for x,y,u in xyu:
	mxy.add(int(y)-int(x),[int(x),u])
mxy.mysort()
#mxy.p()
d = mxy.getDict()
for key in d:
	beu = "-1"
	bex = -1
	for arr in d[key]:
		if beu=="R" and arr[1]=="D":
			ans = min(ans,(arr[0]-bex)*10)
		if beu=="U" and arr[1]=="L":
			ans = min(ans,(arr[0]-bex)*10)
		beu = arr[1]
		bex = arr[0]

if ans==float("inf"):
	print("SAFE")
else:
	print(ans)


Fixed submission:
from collections import defaultdict
from collections import deque
from collections import Counter
import itertools
import math

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n = readInt()
xyu = [readChars() for i in range(n)]

class mydict:
	def __init__(self):
		self.d = {}
	def add(self,key,value):
		if key in self.d:
			self.d[key].append(value)
		else:
			self.d[key]=[value]
	def p(self):
		print("{")
		for key in self.d:
			print(key,self.d[key])
		print("}")
	def mysort(self):
		for i in self.d:
			self.d[i].sort()
	def getDict(self):
		return self.d;

ud = mydict()
rl = mydict()
for x,y,u in xyu:
	if u=="U" or u=="D":
		ud.add(int(x),[int(y),u])
	else:
		rl.add(int(y),[int(x),u])
ud.mysort()
rl.mysort()
#ud.p()
#rl.p()
ans = float("inf")

tar = ud.getDict()
for key in tar:
	beu = "-1"
	bev = "-1"
	for v in tar[key]:
		if beu=="U" and v[1]=="D":
			ans = min(ans,(int(v[0])-int(bev))*5)
		beu = v[1]
		bev = v[0]
tar = rl.getDict()
for key in tar:
	s = ""
	beu = "-1"
	bev = "-1"
	for v in tar[key]:
		if beu=="R" and v[1]=="L":
			ans = min(ans,(int(v[0])-int(bev))*5)
		beu = v[1]
		bev = v[0]

xy = mydict()
for x,y,u in xyu:
	xy.add(int(x)+int(y),[int(x),u])
#xy.p()
xy.mysort()
d = xy.getDict()
for key in d:
	beu = "-1"
	bex = -1
	for arr in d[key]:
		if beu=="R" and arr[1]=="U":
			ans = min(ans,(arr[0]-bex)*10)
		if beu=="D" and arr[1]=="L":
			ans = min(ans,(arr[0]-bex)*10)
		beu = arr[1]
		bex = arr[0]
mxy = mydict()
for x,y,u in xyu:
	mxy.add(int(y)-int(x),[int(x),u])
mxy.mysort()
#mxy.p()
d = mxy.getDict()
for key in d:
	beu = "-1"
	bex = -1
	for arr in d[key]:
		if beu=="R" and arr[1]=="D":
			ans = min(ans,(arr[0]-bex)*10)
		if beu=="U" and arr[1]=="L":
			ans = min(ans,(arr[0]-bex)*10)
		beu = arr[1]
		bex = arr[0]

if ans==float("inf"):
	print("SAFE")
else:
	print(ans)