Buggy python submission (verdict: Time Limit Exceeded):
INF = float("inf")

sc = iter(open(0).read().split())
ni = lambda: int(next(sc))
ns = lambda: next(sc)

LR, UD, RU, LD, LU, RD = [[[] for _ in range(400_100)] for _ in range(6)]

U = []

N = ni()
for i in range(N):
    x, y, u = ni(), ni(), ns()
    U.append(u)

    if u in "LR":
        LR[y].append((x, i))
    if u in "UD":
        UD[x].append((y, i))
    if u in "LD":
        LD[x + y].append((y, i))
    if u in "RU":
        RU[x + y].append((y, i))
    if u in "LU":
        LU[y - x + 200_000].append((x, i))
    if u in "RD":
        RD[y - x + 200_000].append((x, i))

ans = INF
for i in range(400_001):
    LR[i].sort()
    UD[i].sort()
    LD[i].sort()
    RU[i].sort()
    LU[i].sort()
    RD[i].sort()

    ans = min(
        ans,
        5 * min(
            ((LR[i][j + 1][0] - LR[i][j][0])
            for j in range(len(LR[i]) - 1)
            if U[LR[i][j][1]] == "R" and U[LR[i][j + 1][1]] == "L"),
            default=INF
        ),
        5 * min(
            ((UD[i][j + 1][0] - UD[i][j][0])
            for j in range(len(UD[i]) - 1)
            if U[UD[i][j][1]] == "U" and U[UD[i][j + 1][1]] == "D"),
            default=INF
        ),
        10 * min(
            ((LD[i][j + 1][0] - LD[i][j][0])
            for j in range(len(LD[i]) - 1)
            if U[LD[i][j][1]] == "L" and U[LD[i][j + 1][1]] == "D"),
            default=INF
        ),
        10 * min(
            ((RU[i][j + 1][0] - RU[i][j][0])
            for j in range(len(RU[i]) - 1)
            if U[RU[i][j][1]] == "U" and U[RU[i][j + 1][1]] == "R"),
            default=INF
        ),
        10 * min(
            ((LU[i][j + 1][0] - LU[i][j][0])
            for j in range(len(LU[i]) - 1)
            if U[LU[i][j][1]] == "U" and U[LU[i][j + 1][1]] == "L"),
            default=INF
        ),
        10 * min(
            ((RD[i][j + 1][0] - RD[i][j][0])
            for j in range(len(RD[i]) - 1)
            if U[RD[i][j][1]] == "R" and U[RD[i][j + 1][1]] == "D"),
            default=INF
        )
    )

print("SAFE" if ans == INF else ans)

Fixed submission:
INF = float("inf")

sc = iter(open(0).read().split())
ni = lambda: int(next(sc))
ns = lambda: next(sc)

LR, UD, RU, LD, LU, RD = [[[] for _ in range(400_100)] for _ in range(6)]

U = []

N = ni()
for i in range(N):
    x, y, u = ni(), ni(), ns()
    U.append(u)

    if u in "LR":
        LR[y].append((x, i))
    if u in "UD":
        UD[x].append((y, i))
    if u in "LD":
        LD[x + y].append((y, i))
    if u in "RU":
        RU[x + y].append((y, i))
    if u in "LU":
        LU[y - x + 200_000].append((x, i))
    if u in "RD":
        RD[y - x + 200_000].append((x, i))

ans = INF
for i in range(400_001):
    LR[i].sort()
    UD[i].sort()
    LD[i].sort()
    RU[i].sort()
    LU[i].sort()
    RD[i].sort()

    ans = min(
        ans,
        5 * min(
            ((LR[i][j + 1][0] - LR[i][j][0])
            for j in range(len(LR[i]) - 1)
            if U[LR[i][j][1]] == "R" and U[LR[i][j + 1][1]] == "L"),
            default=INF
        ),
        5 * min(
            ((UD[i][j + 1][0] - UD[i][j][0])
            for j in range(len(UD[i]) - 1)
            if U[UD[i][j][1]] == "U" and U[UD[i][j + 1][1]] == "D"),
            default=INF
        ),
        10 * min(
            ((LD[i][j + 1][0] - LD[i][j][0])
            for j in range(len(LD[i]) - 1)
            if U[LD[i][j][1]] == "L" and U[LD[i][j + 1][1]] == "D"),
            default=INF
        ),
        10 * min(
            ((RU[i][j + 1][0] - RU[i][j][0])
            for j in range(len(RU[i]) - 1)
            if U[RU[i][j][1]] == "U" and U[RU[i][j + 1][1]] == "R"),
            default=INF
        ),
        10 * min(
            ((LU[i][j + 1][0] - LU[i][j][0])
            for j in range(len(LU[i]) - 1)
            if U[LU[i][j][1]] == "U" and U[LU[i][j + 1][1]] == "L"),
            default=INF
        ),
        10 * min(
            ((RD[i][j + 1][0] - RD[i][j][0])
            for j in range(len(RD[i]) - 1)
            if U[RD[i][j][1]] == "R" and U[RD[i][j + 1][1]] == "D"),
            default=INF
        )
    )

print("SAFE" if ans == INF else ans)