n = int(input())
xyu = [list(input().split()) for _ in range(n)]

r = []
l = []
u = []
d = []
ans = 10**15

for x,y,U in xyu:
    x = int(x)
    y = int(y)
    if U == 'U':
        u.append((x,y,x+y,x-y))
    elif U == 'R':
        r.append((x,y,x+y,x-y))
    elif U == 'D':
        d.append((x,y,x+y,x-y))
    else:
        l.append((x,y,x+y,x-y))

xr = [[] for _ in range(200010)]
xl = [[] for _ in range(200010)]

for x,y,z,w in r:
    xr[y].append(x)

for x,y,z,w in l:
    xl[y].append(x)

for i in range(len(xr)):
    if xr[i] and xl[i]:
        a = sorted(xr[i])
        b = sorted(xl[i])
        tmp = 0
        tmp2 = 10**10
        flag = True
        for j in range(len(a)):
            if flag:
                while a[j] > b[tmp]:
                    tmp += 1
                    if tmp >= len(b):
                        flag = False
                        break
                if flag:
                    tmp2 = min(tmp2, b[tmp]-a[j])
        ans = min(ans, tmp2*5)

yd = [[] for _ in range(200010)]
yu = [[] for _ in range(200010)]

for x,y,z,w in d:
    yd[x].append(y)

for x,y,z,w in u:
    yu[x].append(y)

for i in range(len(yd)):
    if yd[i] and yu[i]:
        b = sorted(yd[i])
        a = sorted(yu[i])
        tmp = 0
        tmp2 = 10**10
        flag = True
        for j in range(len(a)):
            if flag:
                while a[j] > b[tmp]:
                    tmp += 1
                    if tmp >= len(b):
                        flag = False
                        break
                if flag:
                    tmp2 = min(tmp2, b[tmp]-a[j])
        ans = min(ans, tmp2*5)

zr = [[] for _ in range(400010)]
zu = [[] for _ in range(400010)]

for x,y,z,w in r:
    zr[z].append(x)

for x,y,z,w in u:
    zu[z].append(x)

for i in range(len(zr)):
    if zr[i] and zu[i]:
        b = sorted(zu[i])
        a = sorted(zr[i])
        tmp = 0
        tmp2 = 10**10
        flag = True
        for j in range(len(a)):
            if flag:
                while a[j] > b[tmp]:
                    tmp += 1
                    if tmp >= len(b):
                        flag = False
                        break
                if flag:
                    tmp2 = min(tmp2, b[tmp]-a[j])
        ans = min(ans, tmp2*10)

zd = [[] for _ in range(400010)]
zl = [[] for _ in range(400010)]

for x,y,z,w in d:
    zd[z].append(x)

for x,y,z,w in l:
    zl[z].append(x)

for i in range(len(zd)):
    if zd[i] and zl[i]:
        b = sorted(zl[i])
        a = sorted(zd[i])
        tmp = 0
        tmp2 = 10**10
        flag = True
        for j in range(len(a)):
            if flag:
                while a[j] > b[tmp]:
                    tmp += 1
                    if tmp >= len(b):
                        flag = False
                        break
                if flag:
                    tmp2 = min(tmp2, b[tmp]-a[j])
        ans = min(ans, tmp2*10)

wd = [[] for _ in range(400010)]
wr = [[] for _ in range(400010)]

for x,y,z,w in d:
    wd[w+200005].append(x)

for x,y,z,w in r:
    wr[w+200005].append(x)

for i in range(len(wr)):
    if wd[i] and wr[i]:
        b = sorted(wd[i])
        a = sorted(wr[i])
        tmp = 0
        tmp2 = 10**10
        flag = True
        for j in range(len(a)):
            if flag:
                while a[j] > b[tmp]:
                    tmp += 1
                    if tmp >= len(b):
                        flag = False
                        break
                if flag:
                    tmp2 = min(tmp2, b[tmp]-a[j])
        ans = min(ans, tmp2*10)

wl = [[] for _ in range(400010)]
wu = [[] for _ in range(400010)]

for x,y,z,w in l:
    wl[w+200005].append(x)

for x,y,z,w in u:
    wu[w+200005].append(x)

for i in range(len(wl)):
    if wl[i] and wu[i]:
        b = sorted(wl[i])
        a = sorted(wu[i])
        tmp = 0
        tmp2 = 10**10
        flag = True
        for j in range(len(a)):
            if flag:
                while a[j] > b[tmp]:
                    tmp += 1
                    if tmp >= len(b):
                        flag = False
                        break
                if flag:
                    tmp2 = min(tmp2, b[tmp]-a[j])
        ans = min(ans, tmp2*10)

if ans > 10**10:
    print('SAFE')
else:
    print(ans)


Measured memory_bytes: 346192