Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = lambda: sys.stdin.readline().rstrip('\r\n')

from collections import defaultdict

N = int(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)


Fixed submission:
import sys
input = lambda: sys.stdin.readline().rstrip('\r\n')

from collections import defaultdict

N = int(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)

Buggy python submission (verdict: Runtime Error):
import sys
input = lambda: sys.stdin.readline().rstrip('\r\n')

from collections import defaultdict

N = int(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 = 200000

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)


Fixed submission:
import sys
input = lambda: sys.stdin.readline().rstrip('\r\n')

from collections import defaultdict

N = int(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)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = lambda: sys.stdin.readline().rstrip('\r\n')

from collections import defaultdict

N = int(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
for trial in range(4):
	P.sort()

	rdu, rdd, ry = (defaultdict(lambda: -INF) for _ in range(3))

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

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

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


Fixed submission:
import sys
input = lambda: sys.stdin.readline().rstrip('\r\n')

from collections import defaultdict

N = int(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)