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: 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: 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 X.keys():
        for i in range(4):
            X[k][i].sort()

    for k in 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 Y.keys():
        for i in range(4):
            Y[k][i].sort()

    for k in 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 XYp.keys():
        for i in range(4):
            XYp[k][i].sort()

    for k in 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 XYm.keys():
        for i in range(4):
            XYm[k][i].sort()

    for k in 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()


Measured memory_bytes: 274340