Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())

dic_up = {}
dic_down = {}
dic_hor = {}
dic_ver = {}
lst = []
for i in range(n):
    xi, yi, ui = input().split()
    xi, yi = int(xi), int(yi)
    up = xi - yi
    down = xi + yi
    if (ui, up) in dic_up:
        dic_up[(ui, up)].append(xi)
    else:
        dic_up[(ui, up)] = [xi]
    if (ui, down) in dic_down:
        dic_down[(ui, down)].append(xi)
    else:
        dic_down[(ui, down)] = [xi]
    if (ui, xi) in dic_ver:
        dic_ver[(ui, xi)].append(yi)
    else:
        dic_ver[(ui, xi)] = [yi]
    if (ui, yi) in dic_hor:
        dic_hor[(ui, yi)].append(xi)
    else:
        dic_hor[(ui, yi)] = [xi]
    lst.append([xi, yi, ui, up, down])

for key in dic_up.keys():
    dic_up[key].sort()
for key in dic_down.keys():
    dic_down[key].sort()
for key in dic_hor.keys():
    dic_hor[key].sort()
for key in dic_ver.keys():
    dic_ver[key].sort()

ans = 10**9
for i in range(n):
    xi, yi, ui, up, down = lst[i]
    if ui == 'U':
        if ('D', xi) in dic_ver:
            kouho = 10**9
            for dy in dic_ver[('D', xi)]:
                if dy > yi:
                    kouho = min(kouho, (dy-yi)*5)
            ans = min(ans, kouho)
        if ('L', up) in dic_up:
            kouho = 10**9
            for lx in dic_up[('L', up)]:
                if lx > xi:
                    kouho = min(kouho, (lx-xi)*10)
            ans = min(ans, kouho)
    if ui == 'R':
        if ('U', down) in dic_down:
            kouho = 10**9
            for ux in dic_down[('U', down)]:
                if ux > xi:
                    kouho = min(kouho, (ux-xi)*10)
            ans = min(ans, kouho)
        if ('D', up) in dic_up:
            kouho = 10**9
            for dx in dic_up[('D', up)]:
                if dx > xi:
                    kouho = min(kouho, (dx-xi)*10)
            ans = min(ans, kouho)
        if ('L', yi) in dic_hor:
            kouho = 10**9
            for lx in dic_hor[('L', yi)]:
                if lx > xi:
                    kouho = min(kouho, (lx-xi)*5)
            ans = min(ans, kouho)
    if ui == 'D':
        if ('L', down) in dic_down:
            kouho = 10**9
            for lx in dic_down[('L', down)]:
                if lx > xi:
                    kouho = min(kouho, (lx-xi)*10)
            ans = min(ans, kouho)

if ans == 10**9:
    print('SAFE')
else:
    print(ans)

Fixed submission:
def x_ijo_min(lst, x):
    if lst[0] >= x: return lst[0]
    left = 0 # xより小さい
    right = len(lst) # x以上
    while left+1 < right:
        mid = (left+right) // 2
        if lst[mid] < x:
            left = mid
        else:
            right = mid
    if right == len(lst): return False
    else: return lst[right]

n = int(input())

dic_up = {}
dic_down = {}
dic_hor = {}
dic_ver = {}
lst = []
for i in range(n):
    xi, yi, ui = input().split()
    xi, yi = int(xi), int(yi)
    up = xi - yi
    down = xi + yi
    if (ui, up) in dic_up:
        dic_up[(ui, up)].append(xi)
    else:
        dic_up[(ui, up)] = [xi]
    if (ui, down) in dic_down:
        dic_down[(ui, down)].append(xi)
    else:
        dic_down[(ui, down)] = [xi]
    if (ui, xi) in dic_ver:
        dic_ver[(ui, xi)].append(yi)
    else:
        dic_ver[(ui, xi)] = [yi]
    if (ui, yi) in dic_hor:
        dic_hor[(ui, yi)].append(xi)
    else:
        dic_hor[(ui, yi)] = [xi]
    lst.append([xi, yi, ui, up, down])

for key in dic_up.keys():
    dic_up[key].sort()
for key in dic_down.keys():
    dic_down[key].sort()
for key in dic_hor.keys():
    dic_hor[key].sort()
for key in dic_ver.keys():
    dic_ver[key].sort()

ans = 10**9
for i in range(n):
    xi, yi, ui, up, down = lst[i]
    if ui == 'U':
        if ('D', xi) in dic_ver:
            dy = x_ijo_min(dic_ver[('D', xi)], yi)
            if dy: ans = min(ans, (dy-yi)*5)
        if ('L', up) in dic_up:
            lx = x_ijo_min(dic_up[('L', up)], xi)
            if lx: ans = min(ans, (lx-xi)*10)
    if ui == 'R':
        if ('U', down) in dic_down:
            ux = x_ijo_min(dic_down[('U', down)], xi)
            if ux: ans = min(ans, (ux-xi)*10)
        if ('D', up) in dic_up:
            dx = x_ijo_min(dic_up[('D', up)], xi)
            if dx: ans = min(ans, (dx-xi)*10)
        if ('L', yi) in dic_hor:
            lx = x_ijo_min(dic_hor[('L', yi)], xi)
            if lx: ans = min(ans, (lx-xi)*5)
    if ui == 'D':
        if ('L', down) in dic_down:
            lx = x_ijo_min(dic_down[('L', down)], xi)
            if lx: ans = min(ans, (lx-xi)*10)

if ans == 10**9:
    print('SAFE')
else:
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())

dic_up = {}
dic_down = {}
dic_hor = {}
dic_ver = {}
lst = []
for i in range(n):
    xi, yi, ui = input().split()
    xi, yi = int(xi), int(yi)
    up = xi - yi
    down = xi + yi
    if (ui, up) in dic_up:
        dic_up[(ui, up)].append([xi, yi])
    else:
        dic_up[(ui, up)] = [[xi, yi]]
    if (ui, down) in dic_down:
        dic_down[(ui, down)].append([xi, yi])
    else:
        dic_down[(ui, down)] = [[xi, yi]]
    if (ui, xi) in dic_ver:
        dic_ver[(ui, xi)].append(yi)
    else:
        dic_ver[(ui, xi)] = [yi]
    if (ui, yi) in dic_hor:
        dic_hor[(ui, yi)].append(xi)
    else:
        dic_hor[(ui, yi)] = [xi]
    lst.append([xi, yi, ui, up, down])

ans = 10**9
for i in range(n):
    xi, yi, ui, up, down = lst[i]
    if ui == 'U':
        if ('R', down) in dic_down:
            kouho = 10**9
            for rx, ry in dic_down[('R', down)]:
                if xi > rx:
                    kouho = min(kouho, (xi-rx)*10)
            ans = min(ans, kouho)
        if ('D', xi) in dic_ver:
            kouho = 10**9
            for dy in dic_ver[('D', xi)]:
                if dy > yi:
                    kouho = min(kouho, (dy-yi)*5)
            ans = min(ans, kouho)
        if ('L', up) in dic_up:
            kouho = 10**9
            for lx, ly in dic_up[('L', up)]:
                if lx > xi:
                    kouho = min(kouho, (lx-xi)*10)
            ans = min(ans, kouho)
    if ui == 'R':
        if ('D', up) in dic_up:
            kouho = 10**9
            for dx, dy in dic_up[('D', up)]:
                if dx > xi:
                    kouho = min(kouho, (dx-xi)*10)
            ans = min(ans, kouho)
        if ('L', yi) in dic_hor:
            kouho = 10**9
            for lx in dic_hor[('L', yi)]:
                if lx > xi:
                    kouho = min(kouho, (lx-xi)*5)
            ans = min(ans, kouho)
    if ui == 'D':
        if ('L', down) in dic_down:
            kouho = 10**9
            for lx, ly in dic_down[('L', down)]:
                if lx > xi:
                    kouho = min(kouho, (lx-xi)*10)
            ans = min(ans, kouho)

if ans == 10**9:
    print('SAFE')
else:
    print(ans)

Fixed submission:
def x_ijo_min(lst, x):
    if lst[0] >= x: return lst[0]
    left = 0 # xより小さい
    right = len(lst) # x以上
    while left+1 < right:
        mid = (left+right) // 2
        if lst[mid] < x:
            left = mid
        else:
            right = mid
    if right == len(lst): return False
    else: return lst[right]

n = int(input())

dic_up = {}
dic_down = {}
dic_hor = {}
dic_ver = {}
lst = []
for i in range(n):
    xi, yi, ui = input().split()
    xi, yi = int(xi), int(yi)
    up = xi - yi
    down = xi + yi
    if (ui, up) in dic_up:
        dic_up[(ui, up)].append(xi)
    else:
        dic_up[(ui, up)] = [xi]
    if (ui, down) in dic_down:
        dic_down[(ui, down)].append(xi)
    else:
        dic_down[(ui, down)] = [xi]
    if (ui, xi) in dic_ver:
        dic_ver[(ui, xi)].append(yi)
    else:
        dic_ver[(ui, xi)] = [yi]
    if (ui, yi) in dic_hor:
        dic_hor[(ui, yi)].append(xi)
    else:
        dic_hor[(ui, yi)] = [xi]
    lst.append([xi, yi, ui, up, down])

for key in dic_up.keys():
    dic_up[key].sort()
for key in dic_down.keys():
    dic_down[key].sort()
for key in dic_hor.keys():
    dic_hor[key].sort()
for key in dic_ver.keys():
    dic_ver[key].sort()

ans = 10**9
for i in range(n):
    xi, yi, ui, up, down = lst[i]
    if ui == 'U':
        if ('D', xi) in dic_ver:
            dy = x_ijo_min(dic_ver[('D', xi)], yi)
            if dy: ans = min(ans, (dy-yi)*5)
        if ('L', up) in dic_up:
            lx = x_ijo_min(dic_up[('L', up)], xi)
            if lx: ans = min(ans, (lx-xi)*10)
    if ui == 'R':
        if ('U', down) in dic_down:
            ux = x_ijo_min(dic_down[('U', down)], xi)
            if ux: ans = min(ans, (ux-xi)*10)
        if ('D', up) in dic_up:
            dx = x_ijo_min(dic_up[('D', up)], xi)
            if dx: ans = min(ans, (dx-xi)*10)
        if ('L', yi) in dic_hor:
            lx = x_ijo_min(dic_hor[('L', yi)], xi)
            if lx: ans = min(ans, (lx-xi)*5)
    if ui == 'D':
        if ('L', down) in dic_down:
            lx = x_ijo_min(dic_down[('L', down)], xi)
            if lx: ans = min(ans, (lx-xi)*10)

if ans == 10**9:
    print('SAFE')
else:
    print(ans)