Buggy python submission (verdict: Runtime Error):
import sys
# from collections import Counter

N_MAX = 200000 + 5
INF = 10**9 + 7
sys.setrecursionlimit(N_MAX)
MOD = 10**9 + 7


def main():
    N = int(sys.stdin.readline().rstrip())
    X = [[] for _ in range(200000)]
    Y = [[] for _ in range(200000)]
    A = [[] for _ in range(200000 * 2 + 1)]
    B = [[] for _ in range(200000 * 2 + 1)]

    for i in range(N):
        x, y, u = sys.stdin.readline().rstrip().split()
        x = int(x)
        y = int(y)
        X[x].append((y, u))  # 同一X に含まれるもの
        Y[y].append((x, u))
        A[x + y].append((x, u))
        B[x - y + 200000].append((x, u))

    # 正面衝突
    length = 1000000
    for xs in X:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "D":
                    length = min((y - tmp_y) * 5, length)
                    is_count = False
                if u == "U":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in Y:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min((y - tmp_y) * 5, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in A:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "U":
                    length = min((y - tmp_y) * 10, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min((y - tmp_y) * 10, length)
                    is_count = False
                if u == "D":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in B:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min((y - tmp_y) * 10, length)
                    is_count = False
                if u == "U":
                    tmp_y = y
                    is_count = True
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "D":
                    length = min((y - tmp_y) * 10, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True


    if length == 1000000:
        print("SAFE")
    else:
        print(length)


main()


Fixed submission:
import sys
# from collections import Counter


def main():
    N = int(sys.stdin.readline().rstrip())
    X = [[] for _ in range(200005)]
    Y = [[] for _ in range(200005)]
    A = [[] for _ in range(200005 * 2 + 1)]
    B = [[] for _ in range(200005 * 2 + 1)]

    for i in range(N):
        x, y, u = sys.stdin.readline().rstrip().split()
        x = int(x)
        y = int(y)
        X[x].append((y, u))  # 同一X に含まれるもの
        Y[y].append((x, u))
        A[x + y].append((x, u))
        B[x - y + 200005].append((x, u))

    # 正面衝突
    length = 10000000
    for xs in X:
        if len(xs) >= 2:
            xs.sort()

            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "D":
                    length = min(abs(y - tmp_y) * 5, length)
                    is_count = False
                if u == "U":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in Y:
        if len(xs) >= 2:
            xs.sort()

            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min(abs(y - tmp_y) * 5, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in A:
        if len(xs) >= 2:
            xs.sort()

            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "U":
                    length = min(abs(y - tmp_y) * 10, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min(abs(y - tmp_y) * 10, length)
                    is_count = False
                if u == "D":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in B:
        if len(xs) >= 2:
            xs.sort()

            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min(abs(y - tmp_y) * 10, length)
                    is_count = False
                if u == "U":
                    tmp_y = y
                    is_count = True

            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "D":
                    length = min(abs(y - tmp_y) * 10, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

    if length == 10000000:
        print("SAFE")
    else:
        print(length)


main()

Buggy python submission (verdict: Runtime Error):
import sys
# from collections import Counter

N_MAX = 200000 + 5
INF = 10**9 + 7
sys.setrecursionlimit(N_MAX)
MOD = 10**9 + 7


def main():
    N = int(sys.stdin.readline().rstrip())
    X = [[] for _ in range(200000)]
    Y = [[] for _ in range(200000)]
    A = [[] for _ in range(200000 * 2 + 1)]
    B = [[] for _ in range(200000 * 2 + 1)]

    for i in range(N):
        x, y, u = sys.stdin.readline().rstrip().split()
        x = int(x)
        y = int(y)
        X[x].append((y, u))  # 同一X に含まれるもの
        Y[y].append((x, u))
        A[x + y].append((x, u))
        B[x - y + 200000].append((x, u))

    # 正面衝突
    length = 1000000
    for xs in X:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "D":
                    length = min((y - tmp_y) * 5 , length)
                    is_count = False
                if u == "U":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in Y:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "U":
                    length = min((y - tmp_y) * 5, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in A:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "U":
                    length = min((y - tmp_y) * 10, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in A:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min((y - tmp_y) * 10, length)
                    is_count = False
                if u == "D":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in B:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min((y - tmp_y) * 10, length)
                    is_count = False
                if u == "U":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in B:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "D":
                    length = min((y - tmp_y) * 10, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

    if length == 1000000:
        print("SAFE")
    else:
        print(length)


main()


Fixed submission:
import sys
# from collections import Counter


def main():
    N = int(sys.stdin.readline().rstrip())
    X = [[] for _ in range(200005)]
    Y = [[] for _ in range(200005)]
    A = [[] for _ in range(200005 * 2 + 1)]
    B = [[] for _ in range(200005 * 2 + 1)]

    for i in range(N):
        x, y, u = sys.stdin.readline().rstrip().split()
        x = int(x)
        y = int(y)
        X[x].append((y, u))  # 同一X に含まれるもの
        Y[y].append((x, u))
        A[x + y].append((x, u))
        B[x - y + 200005].append((x, u))

    # 正面衝突
    length = 10000000
    for xs in X:
        if len(xs) >= 2:
            xs.sort()

            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "D":
                    length = min(abs(y - tmp_y) * 5, length)
                    is_count = False
                if u == "U":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in Y:
        if len(xs) >= 2:
            xs.sort()

            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min(abs(y - tmp_y) * 5, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in A:
        if len(xs) >= 2:
            xs.sort()

            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "U":
                    length = min(abs(y - tmp_y) * 10, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min(abs(y - tmp_y) * 10, length)
                    is_count = False
                if u == "D":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in B:
        if len(xs) >= 2:
            xs.sort()

            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min(abs(y - tmp_y) * 10, length)
                    is_count = False
                if u == "U":
                    tmp_y = y
                    is_count = True

            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "D":
                    length = min(abs(y - tmp_y) * 10, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

    if length == 10000000:
        print("SAFE")
    else:
        print(length)


main()

Buggy python submission (verdict: Runtime Error):
import sys
# from collections import Counter

N_MAX = 200000 + 5
INF = 10**9 + 7
sys.setrecursionlimit(N_MAX)
MOD = 10**9 + 7


def main():
    N = int(sys.stdin.readline().rstrip())
    X = [[] for _ in range(200000)]
    Y = [[] for _ in range(200000)]
    A = [[] for _ in range(200000 * 2 + 1)]
    B = [[] for _ in range(200000 * 2 + 1)]

    for i in range(N):
        x, y, u = sys.stdin.readline().rstrip().split()
        x = int(x)
        y = int(y)
        X[x].append((y, u))  # 同一X に含まれるもの
        Y[y].append((x, u))
        A[x + y].append((x, u))
        B[x - y + 200000].append((x, u))

    # 正面衝突
    length = 1000000
    for xs in X:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "D":
                    length = min((y - tmp_y) * 5 , length)
                    is_count = False
                if u == "U":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in Y:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "U":
                    length = min((y - tmp_y) * 5, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in A:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "U":
                    length = min((y - tmp_y) * 10, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in A:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min((y - tmp_y) * 10, length)
                    is_count = False
                if u == "D":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in B:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min((y - tmp_y) * 10, length)
                    is_count = False
                if u == "U":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in B:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            for y, u in xs:
                if is_count and u == "D":
                    length = min((y - tmp_y) * 10, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

    if length == 1000000:
        print("SAFE")
    else:
        print(length)


main()


Fixed submission:
import sys
# from collections import Counter


def main():
    N = int(sys.stdin.readline().rstrip())
    X = [[] for _ in range(200005)]
    Y = [[] for _ in range(200005)]
    A = [[] for _ in range(200005 * 2 + 1)]
    B = [[] for _ in range(200005 * 2 + 1)]

    for i in range(N):
        x, y, u = sys.stdin.readline().rstrip().split()
        x = int(x)
        y = int(y)
        X[x].append((y, u))  # 同一X に含まれるもの
        Y[y].append((x, u))
        A[x + y].append((x, u))
        B[x - y + 200005].append((x, u))

    # 正面衝突
    length = 10000000
    for xs in X:
        if len(xs) >= 2:
            xs.sort()

            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "D":
                    length = min(abs(y - tmp_y) * 5, length)
                    is_count = False
                if u == "U":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in Y:
        if len(xs) >= 2:
            xs.sort()

            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min(abs(y - tmp_y) * 5, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in A:
        if len(xs) >= 2:
            xs.sort()

            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "U":
                    length = min(abs(y - tmp_y) * 10, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min(abs(y - tmp_y) * 10, length)
                    is_count = False
                if u == "D":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in B:
        if len(xs) >= 2:
            xs.sort()

            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min(abs(y - tmp_y) * 10, length)
                    is_count = False
                if u == "U":
                    tmp_y = y
                    is_count = True

            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "D":
                    length = min(abs(y - tmp_y) * 10, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

    if length == 10000000:
        print("SAFE")
    else:
        print(length)


main()

Buggy python submission (verdict: Runtime Error):
import sys
# from collections import Counter

N_MAX = 200000 + 5
INF = 10**9 + 7
sys.setrecursionlimit(N_MAX)
MOD = 10**9 + 7


def main():
    N = int(sys.stdin.readline().rstrip())
    X = [[] for _ in range(200000)]
    Y = [[] for _ in range(200000)]
    A = [[] for _ in range(200000 * 2 + 1)]
    B = [[] for _ in range(200000 * 2 + 1)]

    for i in range(N):
        x, y, u = sys.stdin.readline().rstrip().split()
        x = int(x)
        y = int(y)
        X[x].append((y, u))  # 同一X に含まれるもの
        Y[y].append((x, u))
        A[x + y].append((x, u))
        B[x - y + 200000].append((x, u))

    # 正面衝突
    length = 1000000
    for xs in X:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "D":
                    length = min(y - tmp_y, length)
                    is_count = False
                if u == "U":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in Y:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "U":
                    length = min(y - tmp_y, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in A:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "U":
                    length = min((y - tmp_y) * 2, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in A:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min((y - tmp_y) * 2, length)
                    is_count = False
                if u == "D":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in B:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min((y - tmp_y) * 2, length)
                    is_count = False
                if u == "U":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in B:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            for y, u in xs:
                if is_count and u == "D":
                    length = min((y - tmp_y) * 2, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

    if length == 1000000:
        print("SAFE")
    else:
        print(length * 10 // 2)


main()


Fixed submission:
import sys
# from collections import Counter


def main():
    N = int(sys.stdin.readline().rstrip())
    X = [[] for _ in range(200005)]
    Y = [[] for _ in range(200005)]
    A = [[] for _ in range(200005 * 2 + 1)]
    B = [[] for _ in range(200005 * 2 + 1)]

    for i in range(N):
        x, y, u = sys.stdin.readline().rstrip().split()
        x = int(x)
        y = int(y)
        X[x].append((y, u))  # 同一X に含まれるもの
        Y[y].append((x, u))
        A[x + y].append((x, u))
        B[x - y + 200005].append((x, u))

    # 正面衝突
    length = 10000000
    for xs in X:
        if len(xs) >= 2:
            xs.sort()

            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "D":
                    length = min(abs(y - tmp_y) * 5, length)
                    is_count = False
                if u == "U":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in Y:
        if len(xs) >= 2:
            xs.sort()

            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min(abs(y - tmp_y) * 5, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in A:
        if len(xs) >= 2:
            xs.sort()

            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "U":
                    length = min(abs(y - tmp_y) * 10, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min(abs(y - tmp_y) * 10, length)
                    is_count = False
                if u == "D":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in B:
        if len(xs) >= 2:
            xs.sort()

            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min(abs(y - tmp_y) * 10, length)
                    is_count = False
                if u == "U":
                    tmp_y = y
                    is_count = True

            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "D":
                    length = min(abs(y - tmp_y) * 10, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

    if length == 10000000:
        print("SAFE")
    else:
        print(length)


main()

Buggy python submission (verdict: Runtime Error):
import sys
# from collections import Counter

N_MAX = 200000 + 5
INF = 10**9 + 7
sys.setrecursionlimit(N_MAX)
MOD = 10**9 + 7


def main():
    N = int(sys.stdin.readline().rstrip())
    X = [[] for _ in range(200000)]
    Y = [[] for _ in range(200000)]
    A = [[] for _ in range(200000 * 2 + 1)]
    B = [[] for _ in range(200000 * 2 + 1)]

    for i in range(N):
        x, y, u = sys.stdin.readline().rstrip().split()
        x = int(x)
        y = int(y)
        X[x].append((y, u))  # 同一X に含まれるもの
        Y[i].append((x, u))
        A[x + y].append((x, u))
        B[x - y + 200000].append((x, u))

    # 正面衝突
    length = 1000000
    for xs in X:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "D":
                    length = min(y - tmp_y, length)
                    is_count = False
                if u == "U":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in Y:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "U":
                    length = min(y - tmp_y, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in A:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "U":
                    length = min((y - tmp_y) * 2, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in A:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min((y - tmp_y) * 2, length)
                    is_count = False
                if u == "D":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in B:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min((y - tmp_y) * 2, length)
                    is_count = False
                if u == "U":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in B:
        if len(xs) >= 2:
            xs.sort()
            is_count = False
            for y, u in xs:
                if is_count and u == "D":
                    length = min((y - tmp_y) * 2, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

    if length == 1000000:
        print("SAFE")
    else:
        print(length * 10 // 2)


main()


Fixed submission:
import sys
# from collections import Counter


def main():
    N = int(sys.stdin.readline().rstrip())
    X = [[] for _ in range(200005)]
    Y = [[] for _ in range(200005)]
    A = [[] for _ in range(200005 * 2 + 1)]
    B = [[] for _ in range(200005 * 2 + 1)]

    for i in range(N):
        x, y, u = sys.stdin.readline().rstrip().split()
        x = int(x)
        y = int(y)
        X[x].append((y, u))  # 同一X に含まれるもの
        Y[y].append((x, u))
        A[x + y].append((x, u))
        B[x - y + 200005].append((x, u))

    # 正面衝突
    length = 10000000
    for xs in X:
        if len(xs) >= 2:
            xs.sort()

            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "D":
                    length = min(abs(y - tmp_y) * 5, length)
                    is_count = False
                if u == "U":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in Y:
        if len(xs) >= 2:
            xs.sort()

            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min(abs(y - tmp_y) * 5, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in A:
        if len(xs) >= 2:
            xs.sort()

            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "U":
                    length = min(abs(y - tmp_y) * 10, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min(abs(y - tmp_y) * 10, length)
                    is_count = False
                if u == "D":
                    tmp_y = y
                    is_count = True

    # 正面衝突
    for xs in B:
        if len(xs) >= 2:
            xs.sort()

            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "L":
                    length = min(abs(y - tmp_y) * 10, length)
                    is_count = False
                if u == "U":
                    tmp_y = y
                    is_count = True

            is_count = False
            tmp_y = 0
            for y, u in xs:
                if is_count and u == "D":
                    length = min(abs(y - tmp_y) * 10, length)
                    is_count = False
                if u == "R":
                    tmp_y = y
                    is_count = True

    if length == 10000000:
        print("SAFE")
    else:
        print(length)


main()