mod = 1000000007
eps = 10**-9


def main():
    import sys
    from collections import defaultdict
    input = sys.stdin.readline

    N = int(input())
    Uxy = [[] for _ in range(10**6)]
    Rxy = [[] for _ in range(10**6)]
    Dxy = [[] for _ in range(10**6)]
    Lxy = [[] for _ in range(10**6)]
    Ux_y = [[] for _ in range(10 ** 6)]
    Rx_y = [[] for _ in range(10 ** 6)]
    Dx_y = [[] for _ in range(10 ** 6)]
    Lx_y = [[] for _ in range(10 ** 6)]
    Ux = [[] for _ in range(10 ** 6)]
    Ry = [[] for _ in range(10 ** 6)]
    Dx = [[] for _ in range(10 ** 6)]
    Ly = [[] for _ in range(10 ** 6)]
    Xall = set()
    Yall = set()
    XYall = set()
    X_Yall = set()
    for _ in range(N):
        x, y, d = input().split()
        x = int(x)
        y = int(y)
        Xall.add(x)
        Yall.add(y)
        XYall.add(x+y)
        X_Yall.add(x-y)
        if d == "U":
            Uxy[x+y].append((x, y))
            Ux_y[x-y].append((x, y))
            Ux[x].append(y)
        elif d == "R":
            Rxy[x + y].append((x, y))
            Rx_y[x - y].append((x, y))
            Ry[y].append(x)
        elif d == "D":
            Dxy[x + y].append((x, y))
            Dx_y[x - y].append((x, y))
            Dx[x].append(y)
        else:
            Lxy[x + y].append((x, y))
            Lx_y[x - y].append((x, y))
            Ly[y].append(x)

    ans = 10**9
    # RL
    for y in Yall:
        if len(Ry[y]) > 0 and len(Ly[y]) > 0:
            A = []
            for x in Ry[y]:
                A.append((x, 1))
            for x in Ly[y]:
                A.append((x, -1))
            A.sort(key=lambda p: p[0])
            for i in range(len(A) - 1):
                if A[i][1] == 1 and A[i+1][1] == -1:
                    ans = min(ans, (A[i+1][0] - A[i][0]) * 5)

    # UD
    for x in Xall:
        if len(Ux[x]) > 0 and len(Dx[x]) > 0:
            A = []
            for y in Ux[x]:
                A.append((y, 1))
            for y in Dx[x]:
                A.append((y, -1))
            A.sort(key=lambda p: p[0])
            for i in range(len(A) - 1):
                if A[i][1] == 1 and A[i+1][1] == -1:
                    ans = min(ans, (A[i+1][0] - A[i][0]) * 5)

    # RU
    for xy in XYall:
        A = []
        for x, y in Uxy[xy]:
            A.append((x, y, "U"))
        for x, y in Rxy[xy]:
            A.append((x, y, "R"))
        A.sort(key=lambda p: p[0])
        for i in range(len(A) - 1):
            if A[i][2] == "R" and A[i+1][2] == "U":
                ans = min(ans, (A[i+1][0] - A[i][0]) * 10)

    # DL
    for xy in XYall:
        A = []
        for x, y in Dxy[xy]:
            A.append((x, y, "D"))
        for x, y in Lxy[xy]:
            A.append((x, y, "L"))
        A.sort(key=lambda p: p[0])
        for i in range(len(A) - 1):
            if A[i][2] == "D" and A[i + 1][2] == "L":
                ans = min(ans, (A[i + 1][0] - A[i][0]) * 10)

    # UL
    for x_y in X_Yall:
        A = []
        for x, y in Ux_y[x_y]:
            A.append((x, y, "U"))
        for x, y in Lx_y[x_y]:
            A.append((x, y, "L"))
        A.sort(key=lambda p: p[0])
        for i in range(len(A) - 1):
            if A[i][2] == "U" and A[i + 1][2] == "L":
                ans = min(ans, (A[i + 1][0] - A[i][0]) * 10)

    # DR
    for x_y in X_Yall:
        A = []
        for x, y in Dx_y[x_y]:
            A.append((x, y, "D"))
        for x, y in Rx_y[x_y]:
            A.append((x, y, "R"))
        A.sort(key=lambda p: p[0])
        for i in range(len(A) - 1):
            if A[i][2] == "R" and A[i + 1][2] == "D":
                ans = min(ans, (A[i + 1][0] - A[i][0]) * 10)

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


if __name__ == '__main__':
    main()


Measured memory_bytes: 640964