M-kun is a brilliant air traffic controller.

On the display of his radar, there are N airplanes numbered 1, 2, ..., N, all flying at the same altitude.
Each of the airplanes flies at a constant speed of 0.1 per second in a constant direction. The current coordinates of the airplane numbered i are (X_i, Y_i), and the direction of the airplane is as follows:

* if U_i is `U`, it flies in the positive y direction;
* if U_i is `R`, it flies in the positive x direction;
* if U_i is `D`, it flies in the negative y direction;
* if U_i is `L`, it flies in the negative x direction.



To help M-kun in his work, determine whether there is a pair of airplanes that will collide with each other if they keep flying as they are now.
If there is such a pair, find the number of seconds after which the first collision will happen.
We assume that the airplanes are negligibly small so that two airplanes only collide when they reach the same coordinates simultaneously.

Constraints

* 1 \leq N \leq 200000
* 0 \leq X_i, Y_i \leq 200000
* U_i is `U`, `R`, `D`, or `L`.
* The current positions of the N airplanes, (X_i, Y_i), are all distinct.
* N, X_i, and Y_i are integers.

Input

Input is given from Standard Input in the following format:


N
X_1 Y_1 U_1
X_2 Y_2 U_2
X_3 Y_3 U_3
:
X_N Y_N U_N


Output

If there is a pair of airplanes that will collide with each other if they keep flying as they are now, print an integer representing the number of seconds after which the first collision will happen.
If there is no such pair, print `SAFE`.

Examples

Input

2
11 1 U
11 47 D


Output

230


Input

4
20 30 U
30 20 R
20 10 D
10 20 L


Output

SAFE


Input

8
168 224 U
130 175 R
111 198 D
121 188 L
201 116 U
112 121 R
145 239 D
185 107 L


Output

100
import sys
import heapq, functools, collections
import math, random
from collections import Counter, defaultdict

# available on Google, not available on Codeforces
# import numpy as np
# import scipy


def solve(grid):  # fix inputs here
    console("----- solving ------")
    # grid = [[int(a), int(b), c] for a,b,c in grid]

    n_planes = []
    s_planes = []
    w_planes = []
    e_planes = []

    for a,b,c in grid:
        a, b = int(a), int(b)
        if c == "U":
            n_planes.append((a,b))
        if c == "D":
            s_planes.append((a,b))
        if c == "L":
            w_planes.append((a,b))
        if c == "R":
            e_planes.append((a,b))
        
    console(n_planes)
    console(s_planes)
    console(w_planes)
    console(e_planes)

    # ns collision
    ns = collections.defaultdict(list)
    for a,b in n_planes:
        ns[a].append((b/2,1))
    for a,b in s_planes:
        ns[a].append((b/2,-1))
    
    ns = [(k,sorted(v)) for k,v in ns.items() if len(v) > 1 and len(set(x[1] for x in v)) == 2]
    console("ns", ns)

    # ew collision
    ew = collections.defaultdict(list)
    for a,b in w_planes:
        ew[b].append((a/2,-1))
    for a,b in e_planes:
        ew[b].append((a/2,1))

    ew = [(k,sorted(v)) for k,v in ew.items() if len(v) > 1 and len(set(x[1] for x in v)) == 2]
    console("ew", ew)

    # ne collision
    ne = collections.defaultdict(list)
    for a,b in n_planes:
        val = a+b
        ne[val].append((a,-1))
    for a,b in e_planes:
        val = a+b
        ne[val].append((a,1))

    ne = [(k,sorted(v)) for k,v in ne.items() if len(v) > 1 and len(set(x[1] for x in v)) == 2]
    console("ne", ne)

    # sw collision
    sw = collections.defaultdict(list)
    for a,b in s_planes:
        val = -a-b
        sw[val].append((a,1))
    for a,b in w_planes:
        val = -a-b
        sw[val].append((a,-1))

    sw = [(k,sorted(v)) for k,v in sw.items() if len(v) > 1 and len(set(x[1] for x in v)) == 2]
    console("sw", sw)

    # nw collision
    nw = collections.defaultdict(list)
    for a,b in n_planes:
        val = a-b
        nw[val].append((a,1))
    for a,b in w_planes:
        val = a-b
        nw[val].append((a,-1))

    nw = [(k,sorted(v)) for k,v in nw.items() if len(v) > 1 and len(set(x[1] for x in v)) == 2]
    console("nw", nw)

    # se collision
    se = collections.defaultdict(list)
    for a,b in s_planes:
        val = -a+b
        se[val].append((a,-1))
    for a,b in e_planes:
        val = -a+b
        se[val].append((a,1))

    se = [(k,sorted(v)) for k,v in se.items() if len(v) > 1 and len(set(x[1] for x in v)) == 2]
    console("se", se)

    minres = math.inf

    for c in [ns, ew, ne, sw, nw, se]:
        for _,v in c:
            # console(c)
            # console(v)
            for a,b in zip(v, v[1:]):
                # console(a)
                # console(b)
                if a[1] > b[1]:
                    # pass
                    minres = min(minres, b[0] - a[0])
    
    if minres == math.inf:
        return "SAFE"
    return int(minres*10)


def console(*args):  # the judge will not read these print statement
    # print('\033[36m', *args, '\033[0m', file=sys.stderr)
    return

# fast read all
# sys.stdin.readlines()

for case_num in [1]:
    # read line as a string
    # strr = input()

    # read line as an integer
    nrows = int(input())
    
    # read one line and parse each word as a string
    # lst = input().split()

    # read one line and parse each word as an integer
    # lst = list(map(int,input().split()))

    # read matrix and parse as integers (after reading read nrows)
    # lst = list(map(int,input().split()))
    # nrows = lst[0]  # index containing information, please change
    grid = []
    for _ in range(nrows):
        grid.append(input().split())

    res = solve(grid)  # please change
    
    # Google - case number required
    # print("Case #{}: {}".format(case_num+1, res))

    # Codeforces - no case number required
    print(res)