n = int(input())
xyu = []
for i in range(n):
    x, y, u = map(str, input().split())
    x = int(x)
    y = int(y)
    xyu.append( (x, y, u) )

axisx = [[] for _ in range(200001)]
axisy = [[] for _ in range(200001)]
axislb = [[] for _ in range(400001)]
axislt = [[] for _ in range(400001)]

for x, y, u in xyu:
    if u == 'U' or u == 'D':
        axisx[x].append( (y, u) )
    elif u == 'L' or u == 'R':
        axisy[y].append( (x, u) )
    axislb[x+y].append( (x, u) )
    axislt[x-y+200000].append( (x, u) )

m = 100000000

for i in range(200001):
    if axisx[i]:
        axisx[i] = sorted(axisx[i])
        # print('axisx['+str(i)+']: ', axisx[i])
        d = None
        pos = None
        for y, u in axisx[i]:
            if d is None:
                if u == 'U':
                    d = 'U'
                    pos = y
            elif d == 'U':
                if u == 'U':
                    pos = y
                else:
                    m = min((y - pos)*5, m)
                    d = None
                    pos = None

for i in range(200001):
    if axisy[i]:
        axisy[i] = sorted(axisy[i])
        # print('axisy['+str(i)+']: ', axisy[i])
        d = None
        pos = None
        for x, u in axisy[i]:
            if d is None:
                if u == 'R':
                    d = 'R'
                    pos = x
            elif d == 'R':
                if u == 'R':
                    pos = x
                else:
                    m = min((x - pos)*5, m)
                    d = None
                    pos = None

for i in range(400001):
    if axislb[i]:
        axislb[i] = sorted(axislb[i])
        # print('axislb['+str(i)+']: ', axislb[i])
        d = None
        pos = None
        for x, u in axislb[i]:
            if d is None:
                if u == 'R':
                    d = 'R'
                    pos = x
            elif d == 'R':
                if u == 'R':
                    pos = x
                elif u == 'U':
                    m = min((x - pos)*10, m)
                    d = None
                    pos = None
        d = None
        pos = None
        for x, u in axislb[i]:
            if d is None:
                if u == 'D':
                    d = 'D'
                    pos = x
            elif d == 'D':
                if u == 'D':
                    pos = x
                elif u == 'L':
                    m = min((x - pos)*10, m)
                    d = None
                    pos = None

for i in range(400001):
    if axislt[i]:
        axislt[i] = sorted(axislt[i])
        # print('axislt['+str(i)+']: ', axislt[i])
        d = None
        pos = None
        for x, u in axislt[i]:
            if d is None:
                if u == 'R':
                    d = 'R'
                    pos = x
            elif d == 'R':
                if u == 'R':
                    pos = x
                elif u == 'D':
                    m = min((x - pos)*10, m)
                    d = None
                    pos = None
        d = None
        pos = None
        for x, u in axislt[i]:
            if d is None:
                if u == 'U':
                    d = 'U'
                    pos = x
            elif d == 'U':
                if u == 'U':
                    pos = x
                elif u == 'L':
                    m = min((x - pos)*10, m)
                    d = None
                    pos = None

if m == 100000000:
    print('SAFE')
else:
    print(m)

Measured memory_bytes: 179256