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 java.io.*;
import java.math.*;
import java.util.*;
 
public class Main {
	static final long MOD = 1000000007L;
	static final int INF = 50000000;
	public static void main(String[] args) {
		FastScanner sc = new FastScanner();
		PrintWriter pw = new PrintWriter(System.out);
		
		int N = sc.ni();
		ArrayList<int[]> up = new ArrayList<int[]>();
		ArrayList<int[]> down = new ArrayList<int[]>();
		ArrayList<int[]> left = new ArrayList<int[]>();
		ArrayList<int[]> right = new ArrayList<int[]>();
		for (int i = 0; i < N; i++) {
			int x = sc.ni();
			int y = sc.ni();
			char c  = sc.next().charAt(0);
			int[] pair = new int[] {x,y,c};
			if (c=='U')
				up.add(pair);
			else if (c=='D')
				down.add(pair);
			else if (c=='L')
				left.add(pair);
			else
				right.add(pair);
		}
		int ans = INF;
		PriorityQueue<int[]> pq1 = new PriorityQueue<int[]>(new Comparator<int[]>() {
			@Override
			public int compare(int[] arr1, int[] arr2) {
				return arr1[0]-arr2[0];
			}
		});
		PriorityQueue<int[]> pq2 = new PriorityQueue<int[]>(new Comparator<int[]>() {
			@Override
			public int compare(int[] arr1, int[] arr2) {
				return arr2[0]-arr1[0];
			}
		});
		
		//UR
		for (int[] pair: up)
			pq1.add(pair);
		for (int[] pair: right)
			pq1.add(pair);
		HashMap<Integer,Integer> hm = new HashMap<Integer,Integer>();
		while (!pq1.isEmpty()) {
			int[] pair = pq1.poll();
			int val = pair[1]+pair[0];
			if (pair[2]=='U') {
				if (hm.containsKey(val)) {
					int diff = Math.abs(pair[0]-hm.get(val));
					ans = Math.min(ans,diff);
				}
			} else {
				hm.put(val, pair[0]);
			}
		}
		//UL
		for (int[] pair: up)
			pq2.add(pair);
		for (int[] pair: left)
			pq2.add(pair);
		hm = new HashMap<Integer,Integer>();
		while (!pq2.isEmpty()) {
			int[] pair = pq2.poll();
			int val = pair[1]-pair[0];
			if (pair[2]=='U') {
				if (hm.containsKey(val)) {
					int diff = Math.abs(hm.get(val)-pair[0]);
					ans = Math.min(ans,diff);
				}
			} else {
				hm.put(val, pair[0]);
			}
		}
		//DR
		for (int[] pair: down)
			pq1.add(pair);
		for (int[] pair: right)
			pq1.add(pair);
		hm = new HashMap<Integer,Integer>();
		while (!pq1.isEmpty()) {
			int[] pair = pq1.poll();
			int val = pair[1]-pair[0];
			if (pair[2]=='D') {
				if (hm.containsKey(val)) {
					int diff = Math.abs(pair[0]-hm.get(val));
					ans = Math.min(ans,diff);
				}
			} else {
				hm.put(val, pair[0]);
			}
		}
		
		//DL
		for (int[] pair: down)
			pq2.add(pair);
		for (int[] pair: left)
			pq2.add(pair);
		hm = new HashMap<Integer,Integer>();
		while (!pq2.isEmpty()) {
			int[] pair = pq2.poll();
			int val = pair[1]+pair[0];
			if (pair[2]=='D') {
				if (hm.containsKey(val)) {
					int diff = Math.abs(hm.get(val)-pair[0]);
					ans = Math.min(ans,diff);
				}
			} else {
				hm.put(val, pair[0]);
			}
		}
		
		
		int ans2 = INF;
		//UD
		TreeSet<Integer>[] ts = new TreeSet[200001];
		for (int[] pair: down) {
			if (ts[pair[0]]==null) {
				ts[pair[0]] = new TreeSet<Integer>();
			}
			ts[pair[0]].add(pair[1]);
		}
		for (int[] pair: up) {
			if (ts[pair[0]]!=null) {
				Integer collision = ts[pair[0]].higher(pair[1]);
				if (collision != null) {
					int diff = Math.abs(collision-pair[1]);
					ans2 = Math.min(ans2, diff);
				}
			}
		}
		//LR
		for (int i = 0; i <= 200000; i++)
			ts[i] = null;
		for (int[] pair: right) {
			if (ts[pair[1]]==null) {
				ts[pair[1]] = new TreeSet<Integer>();
			}
			ts[pair[1]].add(pair[0]);
		}
		for (int[] pair: left) {
			if (ts[pair[1]]!=null) {
				Integer collision = ts[pair[1]].lower(pair[0]);
				if (collision != null) {
					int diff = Math.abs(pair[0]-collision);
					ans2 = Math.min(ans2, diff);
				}
			}
		}

		int finalAns = Math.min(10*ans, 5*ans2);
		if (finalAns >= INF) {
			pw.println("SAFE");
		} else {
			pw.println(finalAns);
		}
		
		pw.close();
	}
	
    static class FastScanner { 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastScanner() { 
            br = new BufferedReader(new InputStreamReader(System.in)); 
        } 
  
        String next() { 
            while (st == null || !st.hasMoreElements()) { 
                try { 
                    st = new StringTokenizer(br.readLine());
                } catch (IOException  e) { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        }
        
        int ni() { 
            return Integer.parseInt(next()); 
        }
  
        long nl() { 
            return Long.parseLong(next()); 
        } 
  
        double nd() { 
            return Double.parseDouble(next()); 
        } 
  
        String nextLine() {
            String str = ""; 
            try { 
                str = br.readLine(); 
            } catch (IOException e) {
                e.printStackTrace(); 
            } 
            return str;
        }
    }
}