### BUGGY CODE ###

from collections import defaultdict
from bisect import bisect_right
N = int(input())
XYU = [None]*N
for i in range(N):
    x, y, u = input().split()
    XYU[i] = (int(x), int(y), u)
U, D = defaultdict(list), defaultdict(list)
R, L = defaultdict(list), defaultdict(list) 
Ru, Rd, Lu, Ld = defaultdict(list), defaultdict(list), defaultdict(list), defaultdict(list)
for x, y, u in XYU:
    if u == 'R':
        Ru[x+y].append(x)
        Rd[y-x].append(x)
        R[y].append(x)
    elif u == 'L':
        Lu[y-x].append(x)
        Ld[x+y].append(x)
        L[y].append(x)
    elif u == 'U':
        U[x].append(y)
    else:
        D[x].append(y)
ans = float('inf')
for x in U:
    if len(D[x]) > 0:
        D[x].sort()
        le = len(D[x])
        for y in U[x]:
            idx = bisect_right(D[x], y)
            if idx < le and ans > (D[x][idx]-y)/2:
                ans = (D[x][idx]-y)/2
for y in R:
    if len(L[y]) > 0:
        L[y].sort()
        le = len(L[y])
        for x in R[y]:
            idx = bisect_right(L[y], x)
            if idx < le and ans > (L[y][idx]-x)/2:
                ans = (L[y][idx]-x)/2
for x, y, u in XYU:
    if u == 'U':
        if len(Ru[x+y]) > 0:
            for _x in Ru[x+y]:
                if _x < x and ans > x-_x:
                    ans = x-_x
        if len(Lu[y-x]) > 0:
            for _x in Lu[y-x]:
                if x < _x and ans > _x-x:
                    ans = _x-x
    elif u == 'D':
        if len(Rd[y-x]) > 0:
            for _x in Rd[y-x]:
                if x > _x and ans > x-_x:
                    ans = x-_x
        if len(Ld[x+y]) > 0:
            for _x in Ld[x+y]:
                if x < _x and ans > _x-x:
                    ans = _x-x
if ans == float('inf'):
    print('SAFE')
else:
    print(int(ans*10))

### FIXED CODE ###

from collections import defaultdict
from bisect import bisect_right
N = int(input())
XYU = [None]*N
for i in range(N):
    x, y, u = input().split()
    XYU[i] = (int(x), int(y), u)
U, D = defaultdict(list), defaultdict(list)
R, L = defaultdict(list), defaultdict(list) 
Ru, Rd, Lu, Ld = defaultdict(list), defaultdict(list), defaultdict(list), defaultdict(list)
for x, y, u in XYU:
    if u == 'R':
        Ru[x+y].append(x)
        Rd[y-x].append(x)
        R[y].append(x)
    elif u == 'L':
        Lu[y-x].append(x)
        Ld[x+y].append(x)
        L[y].append(x)
    elif u == 'U':
        U[x].append(y)
    else:
        D[x].append(y)
ans = float('inf')
for x in U:
    if len(D[x]) > 0:
        D[x].sort()
        le = len(D[x])
        for y in U[x]:
            idx = bisect_right(D[x], y)
            if idx < le and ans > (D[x][idx]-y)/2:
                ans = (D[x][idx]-y)/2
for y in R:
    if len(L[y]) > 0:
        L[y].sort()
        le = len(L[y])
        for x in R[y]:
            idx = bisect_right(L[y], x)
            if idx < le and ans > (L[y][idx]-x)/2:
                ans = (L[y][idx]-x)/2
for k in Ru:
    Ru[k].sort()
for k in Lu:
    Lu[k].sort()
for k in Rd:
    Rd[k].sort()
for k in Ld:
    Ld[k].sort()
for x, y, u in XYU:
    if u == 'U':
        if len(Ru[x+y]) > 0:
            idx = bisect_right(Ru[x+y], x)-1
            if 0 <= idx < len(Ru[x+y]):
                ans = min(ans, x-Ru[x+y][idx])
        if len(Lu[y-x]) > 0:
            idx = bisect_right(Lu[y-x], x)
            if idx < len(Lu[y-x]):
                ans = min(ans, Lu[y-x][idx]-x)
    elif u == 'D':
        if len(Rd[y-x]) > 0:
            idx = bisect_right(Rd[y-x], x)-1
            if 0 <= idx < len(Rd[y-x]):
                ans = min(ans, x-Rd[y-x][idx])
        if len(Ld[x+y]) > 0:
            idx = bisect_right(Ld[x+y], x)
            if idx < len(Ld[x+y]):
                ans = min(ans, Ld[x+y][idx]-x)
if ans == float('inf'):
    print('SAFE')
else:
    print(int(ans*10))