import sys

from collections import defaultdict

import bisect



read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines



in_n = lambda: int(readline())

in_nn = lambda: list(map(int, readline().split()))

in_s = lambda: readline().rstrip().decode('utf-8')

in_nl = lambda: list(map(int, readline().split()))

in_nl2 = lambda H: [in_nl() for _ in range(H)]

in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]

in_map2 = lambda H: [in_map() for _ in range(H)]

in_all = lambda: list(map(int, read().split()))





def main():



    N = in_n()



    X = defaultdict(lambda: [[] for _ in range(4)])

    Y = defaultdict(lambda: [[] for _ in range(4)])

    XYp = defaultdict(lambda: [[] for _ in range(4)])

    XYm = defaultdict(lambda: [[] for _ in range(4)])

    urdl = ['U', 'R', 'D', 'L']



    for i in range(N):

        x, y, u = in_s().split()

        x, y = int(x), int(y)

        u = urdl.index(u)



        X[y][u].append(x)

        Y[x][u].append(y)

        XYp[x + y][u].append(x)

        XYm[x - y][u].append(x)



    INF = 10**18

    ans = INF



    for k in list(X.keys()):

        for i in range(4):

            X[k][i].sort()



    for k in list(X.keys()):

        for i in range(len(X[k][1])):

            now = X[k][1][i]

            j = bisect.bisect_left(X[k][3], now)



            if j < len(X[k][3]):

                next = X[k][3][j]

                ans = min(ans, (next - now) * 10 // 2)



    for k in list(Y.keys()):

        for i in range(4):

            Y[k][i].sort()



    for k in list(Y.keys()):

        for i in range(len(Y[k][0])):

            now = Y[k][0][i]

            j = bisect.bisect_left(Y[k][2], now)



            if j < len(Y[k][2]):

                next = Y[k][2][j]

                ans = min(ans, (next - now) * 10 // 2)



    for k in list(XYp.keys()):

        for i in range(4):

            XYp[k][i].sort()



    for k in list(XYp.keys()):



        # if len(XYp[k][1]) > 0 and len(XYp[k][0]) > 0:

        #     print(k, XYp[k])

        # if len(XYp[k][2]) > 0 and len(XYp[k][3]) > 0:

        #     print(k, XYp[k])



        for i in range(len(XYp[k][1])):

            now = XYp[k][1][i]

            j = bisect.bisect_left(XYp[k][0], now)

            if j < len(XYp[k][0]):

                next = XYp[k][0][j]

                ans = min(ans, (next - now) * 10)



        for i in range(len(XYp[k][2])):

            now = XYp[k][2][i]

            j = bisect.bisect_left(XYp[k][3], now)

            if j < len(XYp[k][3]):

                next = XYp[k][3][j]

                ans = min(ans, (next - now) * 10)



    for k in list(XYm.keys()):

        for i in range(4):

            XYm[k][i].sort()



    for k in list(XYm.keys()):



        # if len(XYm[k][1]) > 0 and len(XYm[k][2]) > 0:

        #     print(k, XYm[k])

        # if len(XYm[k][0]) > 0 and len(XYm[k][3]) > 0:

        #     print(k, XYm[k])



        for i in range(len(XYm[k][1])):

            now = XYm[k][1][i]

            j = bisect.bisect_left(XYm[k][2], now)

            if j < len(XYm[k][2]):

                next = XYm[k][2][j]

                ans = min(ans, (next - now) * 10)



        for i in range(len(XYm[k][0])):

            now = XYm[k][0][i]

            j = bisect.bisect_left(XYm[k][3], now)

            if j < len(XYm[k][3]):

                next = XYm[k][3][j]

                ans = min(ans, (next - now) * 10)



    # print(X)

    # print(Y)

    # print(XYp)

    # print(XYm)



    if ans == INF:

        print('SAFE')

    else:

        print(ans)





if __name__ == '__main__':

    main()

from collections import defaultdict

from bisect import bisect_left



INF = 10**18





def calc(data, p, q):



    ret = INF

    for k in list(data.keys()):

        for i in range(len(data[k][p])):

            now = data[k][p][i]

            j = bisect_left(data[k][q], now)



            if j < len(data[k][q]):

                next = data[k][q][j]

                ret = min(ret, (next - now) * 10)



    return ret





def main():



    N = int(eval(input()))



    X = defaultdict(lambda: [[] for _ in range(4)])

    Y = defaultdict(lambda: [[] for _ in range(4)])

    XY_plus = defaultdict(lambda: [[] for _ in range(4)])

    XY_minus = defaultdict(lambda: [[] for _ in range(4)])

    URDL = ['U', 'R', 'D', 'L']



    for i in range(N):

        x, y, u = input().split()

        x, y = int(x), int(y)

        u = URDL.index(u)



        X[y][u].append(x)

        Y[x][u].append(y)

        XY_plus[x + y][u].append(x)

        XY_minus[x - y][u].append(x)



    for s in (X, Y, XY_plus, XY_minus):

        for k in list(s.keys()):

            for i in range(4):

                s[k][i].sort()



    ans = INF

    ans = min(ans, calc(X, 1, 3) // 2)

    ans = min(ans, calc(Y, 0, 2) // 2)

    ans = min(ans, calc(XY_plus, 1, 0))

    ans = min(ans, calc(XY_plus, 2, 3))

    ans = min(ans, calc(XY_minus, 1, 2))

    ans = min(ans, calc(XY_minus, 0, 3))



    if ans >= 5000000:

        print('SAFE')

    else:

        print(ans)





if __name__ == '__main__':

    main()

import sys
from collections import defaultdict
import bisect

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
in_n = lambda: int(readline())
in_nn = lambda: list(map(int, readline().split()))
in_s = lambda: readline().rstrip().decode("utf-8")
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord(".") for s in readline() if s != ord("\n")]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: list(map(int, read().split()))


def main():
    N = in_n()
    X = defaultdict(lambda: [[] for _ in range(4)])
    Y = defaultdict(lambda: [[] for _ in range(4)])
    XYp = defaultdict(lambda: [[] for _ in range(4)])
    XYm = defaultdict(lambda: [[] for _ in range(4)])
    urdl = ["U", "R", "D", "L"]
    for i in range(N):
        x, y, u = in_s().split()
        x, y = int(x), int(y)
        u = urdl.index(u)
        X[y][u].append(x)
        Y[x][u].append(y)
        XYp[x + y][u].append(x)
        XYm[x - y][u].append(x)
    INF = 10**18
    ans = INF
    for k in list(X.keys()):
        for i in range(4):
            X[k][i].sort()
    for k in list(X.keys()):
        for i in range(len(X[k][1])):
            now = X[k][1][i]
            j = bisect.bisect_left(X[k][3], now)
            if j < len(X[k][3]):
                next = X[k][3][j]
                ans = min(ans, (next - now) * 10 // 2)
    for k in list(Y.keys()):
        for i in range(4):
            Y[k][i].sort()
    for k in list(Y.keys()):
        for i in range(len(Y[k][0])):
            now = Y[k][0][i]
            j = bisect.bisect_left(Y[k][2], now)
            if j < len(Y[k][2]):
                next = Y[k][2][j]
                ans = min(ans, (next - now) * 10 // 2)
    for k in list(XYp.keys()):
        for i in range(4):
            XYp[k][i].sort()
    for k in list(XYp.keys()):
        # if len(XYp[k][1]) > 0 and len(XYp[k][0]) > 0:
        #     print(k, XYp[k])
        # if len(XYp[k][2]) > 0 and len(XYp[k][3]) > 0:
        #     print(k, XYp[k])
        for i in range(len(XYp[k][1])):
            now = XYp[k][1][i]
            j = bisect.bisect_left(XYp[k][0], now)
            if j < len(XYp[k][0]):
                next = XYp[k][0][j]
                ans = min(ans, (next - now) * 10)
        for i in range(len(XYp[k][2])):
            now = XYp[k][2][i]
            j = bisect.bisect_left(XYp[k][3], now)
            if j < len(XYp[k][3]):
                next = XYp[k][3][j]
                ans = min(ans, (next - now) * 10)
    for k in list(XYm.keys()):
        for i in range(4):
            XYm[k][i].sort()
    for k in list(XYm.keys()):
        # if len(XYm[k][1]) > 0 and len(XYm[k][2]) > 0:
        #     print(k, XYm[k])
        # if len(XYm[k][0]) > 0 and len(XYm[k][3]) > 0:
        #     print(k, XYm[k])
        for i in range(len(XYm[k][1])):
            now = XYm[k][1][i]
            j = bisect.bisect_left(XYm[k][2], now)
            if j < len(XYm[k][2]):
                next = XYm[k][2][j]
                ans = min(ans, (next - now) * 10)
        for i in range(len(XYm[k][0])):
            now = XYm[k][0][i]
            j = bisect.bisect_left(XYm[k][3], now)
            if j < len(XYm[k][3]):
                next = XYm[k][3][j]
                ans = min(ans, (next - now) * 10)
    # print(X)
    # print(Y)
    # print(XYp)
    # print(XYm)
    if ans == INF:
        print("SAFE")
    else:
        print(ans)


if __name__ == "__main__":
    main()

from collections import defaultdict
from bisect import bisect_left

INF = 10**18


def calc(data, p, q):
    ret = INF
    for k in list(data.keys()):
        for i in range(len(data[k][p])):
            now = data[k][p][i]
            j = bisect_left(data[k][q], now)
            if j < len(data[k][q]):
                next = data[k][q][j]
                ret = min(ret, (next - now) * 10)
    return ret


def main():
    N = int(eval(input()))
    X = defaultdict(lambda: [[] for _ in range(4)])
    Y = defaultdict(lambda: [[] for _ in range(4)])
    XY_plus = defaultdict(lambda: [[] for _ in range(4)])
    XY_minus = defaultdict(lambda: [[] for _ in range(4)])
    URDL = ["U", "R", "D", "L"]
    for i in range(N):
        x, y, u = input().split()
        x, y = int(x), int(y)
        u = URDL.index(u)
        X[y][u].append(x)
        Y[x][u].append(y)
        XY_plus[x + y][u].append(x)
        XY_minus[x - y][u].append(x)
    for s in (X, Y, XY_plus, XY_minus):
        for k in list(s.keys()):
            for i in range(4):
                s[k][i].sort()
    ans = INF
    ans = min(ans, calc(X, 1, 3) // 2)
    ans = min(ans, calc(Y, 0, 2) // 2)
    ans = min(ans, calc(XY_plus, 1, 0))
    ans = min(ans, calc(XY_plus, 2, 3))
    ans = min(ans, calc(XY_minus, 1, 2))
    ans = min(ans, calc(XY_minus, 0, 3))
    if ans >= 5000000:
        print("SAFE")
    else:
        print(ans)


if __name__ == "__main__":
    main()

-import sys -import bisect +from bisect import bisect_left -read = sys.stdin.buffer.read -readline = sys.stdin.buffer.readline -readlines = sys.stdin.buffer.readlines -in_n = lambda: int(readline()) -in_nn = lambda: list(map(int, readline().split())) -in_s = lambda: readline().rstrip().decode("utf-8") -in_nl = lambda: list(map(int, readline().split())) -in_nl2 = lambda H: [in_nl() for _ in range(H)] -in_map = lambda: [s == ord(".") for s in readline() if s != ord("\n")] -in_map2 = lambda H: [in_map() for _ in range(H)] -in_all = lambda: list(map(int, read().split())) +INF = 10**18 + + +def calc(data, p, q): +    ret = INF +    for k in list(data.keys()): +        for i in range(len(data[k][p])): +            now = data[k][p][i] +            j = bisect_left(data[k][q], now) +            if j < len(data[k][q]): +                next = data[k][q][j] +                ret = min(ret, (next - now) * 10) +    return ret -    N = in_n() +    N = int(eval(input())) -    XYp = defaultdict(lambda: [[] for _ in range(4)]) -    XYm = defaultdict(lambda: [[] for _ in range(4)]) -    urdl = ["U", "R", "D", "L"] +    XY_plus = defaultdict(lambda: [[] for _ in range(4)]) +    XY_minus = defaultdict(lambda: [[] for _ in range(4)]) +    URDL = ["U", "R", "D", "L"] -        x, y, u = in_s().split() +        x, y, u = input().split() -        u = urdl.index(u) +        u = URDL.index(u) -        XYp[x + y][u].append(x) -        XYm[x - y][u].append(x) -    INF = 10**18 +        XY_plus[x + y][u].append(x) +        XY_minus[x - y][u].append(x) +    for s in (X, Y, XY_plus, XY_minus): +        for k in list(s.keys()): +            for i in range(4): +                s[k][i].sort() -    for k in list(X.keys()): -        for i in range(4): -            X[k][i].sort() -    for k in list(X.keys()): -        for i in range(len(X[k][1])): -            now = X[k][1][i] -            j = bisect.bisect_left(X[k][3], now) -            if j < len(X[k][3]): -                next = X[k][3][j] -                ans = min(ans, (next - now) * 10 // 2) -    for k in list(Y.keys()): -        for i in range(4): -            Y[k][i].sort() -    for k in list(Y.keys()): -        for i in range(len(Y[k][0])): -            now = Y[k][0][i] -            j = bisect.bisect_left(Y[k][2], now) -            if j < len(Y[k][2]): -                next = Y[k][2][j] -                ans = min(ans, (next - now) * 10 // 2) -    for k in list(XYp.keys()): -        for i in range(4): -            XYp[k][i].sort() -    for k in list(XYp.keys()): -        # if len(XYp[k][1]) > 0 and len(XYp[k][0]) > 0: -        #     print(k, XYp[k]) -        # if len(XYp[k][2]) > 0 and len(XYp[k][3]) > 0: -        #     print(k, XYp[k]) -        for i in range(len(XYp[k][1])): -            now = XYp[k][1][i] -            j = bisect.bisect_left(XYp[k][0], now) -            if j < len(XYp[k][0]): -                next = XYp[k][0][j] -                ans = min(ans, (next - now) * 10) -        for i in range(len(XYp[k][2])): -            now = XYp[k][2][i] -            j = bisect.bisect_left(XYp[k][3], now) -            if j < len(XYp[k][3]): -                next = XYp[k][3][j] -                ans = min(ans, (next - now) * 10) -    for k in list(XYm.keys()): -        for i in range(4): -            XYm[k][i].sort() -    for k in list(XYm.keys()): -        # if len(XYm[k][1]) > 0 and len(XYm[k][2]) > 0: -        #     print(k, XYm[k]) -        # if len(XYm[k][0]) > 0 and len(XYm[k][3]) > 0: -        #     print(k, XYm[k]) -        for i in range(len(XYm[k][1])): -            now = XYm[k][1][i] -            j = bisect.bisect_left(XYm[k][2], now) -            if j < len(XYm[k][2]): -                next = XYm[k][2][j] -                ans = min(ans, (next - now) * 10) -        for i in range(len(XYm[k][0])): -            now = XYm[k][0][i] -            j = bisect.bisect_left(XYm[k][3], now) -            if j < len(XYm[k][3]): -                next = XYm[k][3][j] -                ans = min(ans, (next - now) * 10) -    # print(X) -    # print(Y) -    # print(XYp) -    # print(XYm) -    if ans == INF: +    ans = min(ans, calc(X, 1, 3) // 2) +    ans = min(ans, calc(Y, 0, 2) // 2) +    ans = min(ans, calc(XY_plus, 1, 0)) +    ans = min(ans, calc(XY_plus, 2, 3)) +    ans = min(ans, calc(XY_minus, 1, 2)) +    ans = min(ans, calc(XY_minus, 0, 3)) +    if ans >= 5000000: