Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
U = []
D = []
R = []
L = []
U_ = defaultdict(list)
U__ = defaultdict(list)
D_ = defaultdict(list)
D__ = defaultdict(list)
Ux = defaultdict(list)
Dx = defaultdict(list)
Ry = defaultdict(list)
Ly = defaultdict(list)
for _ in range(n):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    if u == 'U':
        Ux[x].append(y)
        U_[x+y].append((x, y))
        U__[x-y].append((x, y))
        U.append((x, y))
    elif u == 'D':
        Dx[x].append(y)
        D_[x+y].append((x, y))
        D__[x-y].append((x, y))
        D.append((x, y))
    elif u == 'R':
        Ry[y].append(x)
        R.append((x, y))
    else:
        Ly[y].append(x)
        L.append((x, y))

for i in Ux.values():
    i.sort()
for i in Ry.values():
    i.sort()

ans = inf
for i, j in Dx.items():
    for k in j:
        r = br(Ux[i], k)
        if r != 0:
            ans = min(ans, int((k - Ux[i][r-1]) / 2 * 10))
for i, j in Ly.items():
    for k in j:
        r = br(Ry[i], k)
        if r != 0:
            ans = min(ans, int((k - Ry[i][r-1]) / 2 * 10))
for (i, j) in R:
    for (k, l) in D__[i-j]:
        if l - j >= 0:
            ans = min(ans, (l - j) * 10)

for (i, j) in R:
    for (k, l) in U_[i+j]:
        if j - l >= 0:
            ans = min(ans, (j - l) * 10)

for (i, j) in L:
    for (k, l) in U__[i-j]:
        if i - k >= 0:
            ans = min(ans, (i - k) * 10)

for (i, j) in L:
    for (k, l) in D_[i+j]:
        if i - k >= 0:
            ans = min(ans, (i - k) * 10)

if ans == inf:
    print('SAFE')
else:
    print(ans)    


Fixed submission:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
R = []
L = []
U_ = defaultdict(list)
U__ = defaultdict(list)
D_ = defaultdict(list)
D__ = defaultdict(list)
Ux = defaultdict(list)
Dx = defaultdict(list)
Ry = defaultdict(list)
Ly = defaultdict(list)
for _ in range(n):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    if u == 'U':
        Ux[x].append(y)
        U_[x+y].append((x, y))
        U__[x-y].append((x, y))
    elif u == 'D':
        Dx[x].append(y)
        D_[x+y].append((x, y))
        D__[x-y].append((x, y))
    elif u == 'R':
        Ry[y].append(x)
        R.append((x, y))
    else:
        Ly[y].append(x)
        L.append((x, y))

for i in Ux.values():
    i.sort()
for i in Ry.values():
    i.sort()

ans = inf
for i, j in Dx.items():
    for k in j:
        r = br(Ux[i], k)
        if r != 0:
            ans = min(ans, int((k - Ux[i][r-1]) / 2 * 10))
for i, j in Ly.items():
    for k in j:
        r = br(Ry[i], k)
        if r != 0:
            ans = min(ans, int((k - Ry[i][r-1]) / 2 * 10))

for i in U_.values():
    i.sort(reverse=True)
for i in U__.values():
    i.sort(reverse=True)
for i in D_.values():
    i.sort(reverse=True)
for i in D__.values():
    i.sort(reverse=True)

for (i, j) in R:
    for (k, l) in D__[i-j]:
        if l - j >= 0:
            ans = min(ans, (l - j) * 10)
        else:
            break

for (i, j) in R:
    for (k, l) in U_[i+j]:
        if j - l >= 0:
            ans = min(ans, (j - l) * 10)
        else:
            break

for (i, j) in L:
    for (k, l) in U__[i-j]:
        if i - k >= 0:
            ans = min(ans, (i - k) * 10)
        else:
            break

for (i, j) in L:
    for (k, l) in D_[i+j]:
        if i - k >= 0:
            ans = min(ans, (i - k) * 10)
        else:
            break


if ans == inf:
    print('SAFE')
else:
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
U, D, R, L = [], [], [], []
Ux = defaultdict(list)
Dx = defaultdict(list)
Ry = defaultdict(list)
Ly = defaultdict(list)
for _ in range(n):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    if u == 'U':
        Ux[x].append(y)
        U.append((x, y))
    elif u == 'D':
        Dx[x].append(y)
        D.append((x, y))
    elif u == 'R':
        Ry[y].append(x)
        R.append((x, y))
    else:
        Ly[y].append(x)
        L.append((x, y))

for i in Ux.values():
    i.sort()
for i in Ry.values():
    i.sort()

ans = inf
for i, j in Dx.items():
    for k in j:
        r = br(Ux[i], k)
        if r != 0:
            ans = min(ans, int((k - Ux[i][r-1]) / 2 * 10))
for i, j in Ly.items():
    for k in j:
        r = br(Ry[i], k)
        if r != 0:
            ans = min(ans, int((k - Ry[i][r-1]) / 2 * 10))
for (i, j) in R:
    for (k, l) in D:
        if l - j == k - i and l - j >= 0:
            ans = min(ans, (l - j) * 10)

for (i, j) in R:
    for (k, l) in U:
        if j - l == k - i and j - l >= 0:
            ans = min(ans, (j - l) * 10)

for (i, j) in L:
    for (k, l) in U:
        if i - k == j - l and i - k >= 0:
            ans = min(ans, (i - k) * 10)

for (i, j) in L:
    for (k, l) in D:
        if i - k == l - j and i - k >= 0:
            ans = min(ans, (i - k) * 10)

if ans == inf:
    print('SAFE')
else:
    print(ans)    


Fixed submission:
#!/usr/bin/env python3

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
R = []
L = []
U_ = defaultdict(list)
U__ = defaultdict(list)
D_ = defaultdict(list)
D__ = defaultdict(list)
Ux = defaultdict(list)
Dx = defaultdict(list)
Ry = defaultdict(list)
Ly = defaultdict(list)
for _ in range(n):
    x, y, u = input().split()
    x = int(x)
    y = int(y)
    if u == 'U':
        Ux[x].append(y)
        U_[x+y].append((x, y))
        U__[x-y].append((x, y))
    elif u == 'D':
        Dx[x].append(y)
        D_[x+y].append((x, y))
        D__[x-y].append((x, y))
    elif u == 'R':
        Ry[y].append(x)
        R.append((x, y))
    else:
        Ly[y].append(x)
        L.append((x, y))

for i in Ux.values():
    i.sort()
for i in Ry.values():
    i.sort()

ans = inf
for i, j in Dx.items():
    for k in j:
        r = br(Ux[i], k)
        if r != 0:
            ans = min(ans, int((k - Ux[i][r-1]) / 2 * 10))
for i, j in Ly.items():
    for k in j:
        r = br(Ry[i], k)
        if r != 0:
            ans = min(ans, int((k - Ry[i][r-1]) / 2 * 10))

for i in U_.values():
    i.sort(reverse=True)
for i in U__.values():
    i.sort(reverse=True)
for i in D_.values():
    i.sort(reverse=True)
for i in D__.values():
    i.sort(reverse=True)

for (i, j) in R:
    for (k, l) in D__[i-j]:
        if l - j >= 0:
            ans = min(ans, (l - j) * 10)
        else:
            break

for (i, j) in R:
    for (k, l) in U_[i+j]:
        if j - l >= 0:
            ans = min(ans, (j - l) * 10)
        else:
            break

for (i, j) in L:
    for (k, l) in U__[i-j]:
        if i - k >= 0:
            ans = min(ans, (i - k) * 10)
        else:
            break

for (i, j) in L:
    for (k, l) in D_[i+j]:
        if i - k >= 0:
            ans = min(ans, (i - k) * 10)
        else:
            break


if ans == inf:
    print('SAFE')
else:
    print(ans)