Problem:
Score: 600 points

Problem Statement

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.

Sample Input 1

2
11 1 U
11 47 D

Sample Output 1

230

If the airplanes keep flying as they are now, two airplanes numbered 1 and 2 will reach the coordinates (11, 24) simultaneously and collide.

Sample Input 2

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

Sample Output 2

SAFE

No pair of airplanes will collide.

Sample Input 3

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

Sample Output 3

100

Solution in Java (OpenJDK 11.0.6) (judge verdict: Accepted)

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeSet;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		int n = Integer.parseInt(sa[0]);
		Obj[] arr = new Obj[n];
		for (int i = 0; i < n; i++) {
			sa = br.readLine().split(" ");
			Obj o = new Obj();
			o.x = Integer.parseInt(sa[0]);
			o.y = Integer.parseInt(sa[1]);
			o.u = sa[2];
			arr[i] = o;
		}
		br.close();

		int ans = Integer.MAX_VALUE;

		// 上下
		Map<Integer, TreeSet<Obj>> map1 = new HashMap<>();
		Map<Integer, TreeSet<Obj>> map2 = new HashMap<>();
		for (Obj o : arr) {
			if ("U".equals(o.u)) {
				TreeSet<Obj> set = map1.get(o.x);
				if (set == null) {
					 set = new TreeSet<Obj>((o1, o2) -> o1.y - o2.y);
					 map1.put(o.x, set);
				}
				set.add(o);
			}
			if ("D".equals(o.u)) {
				TreeSet<Obj> set = map2.get(o.x);
				if (set == null) {
					 set = new TreeSet<Obj>((o1, o2) -> o1.y - o2.y);
					 map2.put(o.x, set);
				}
				set.add(o);
			}
		}
		for (Integer x : map1.keySet()) {
			TreeSet<Obj> set1 = map1.get(x);
			TreeSet<Obj> set2 = map2.get(x);
			if (set2 != null) {
				for (Obj o1 : set1) {
					Obj o2 = set2.higher(o1);
					if (o2 != null) {
						int val = (o2.y - o1.y) * 5;
						ans = Math.min(ans, val);
					}
				}
			}
		}

		// 左右
		map1 = new HashMap<>();
		map2 = new HashMap<>();
		for (Obj o : arr) {
			if ("R".equals(o.u)) {
				TreeSet<Obj> set = map1.get(o.y);
				if (set == null) {
					 set = new TreeSet<Obj>((o1, o2) -> o1.x - o2.x);
					 map1.put(o.y, set);
				}
				set.add(o);
			}
			if ("L".equals(o.u)) {
				TreeSet<Obj> set = map2.get(o.y);
				if (set == null) {
					 set = new TreeSet<Obj>((o1, o2) -> o1.x - o2.x);
					 map2.put(o.y, set);
				}
				set.add(o);
			}
		}
		for (Integer y : map1.keySet()) {
			TreeSet<Obj> set1 = map1.get(y);
			TreeSet<Obj> set2 = map2.get(y);
			if (set2 != null) {
				for (Obj o1 : set1) {
					Obj o2 = set2.higher(o1);
					if (o2 != null) {
						int val = (o2.x - o1.x) * 5;
						ans = Math.min(ans, val);
					}
				}
			}
		}

		// 下左
		Map<Integer, TreeSet<Obj>> mapD = make(arr, "D", true);
		Map<Integer, TreeSet<Obj>> mapL = make(arr, "L", true);
		for (Integer v : mapD.keySet()) {
			TreeSet<Obj> set = mapD.get(v);
			TreeSet<Obj> set2 = mapL.get(v);
			if (set2 != null) {
				for (Obj o : set) {
					Obj o2 = set2.higher(o);
					if (o2 != null) {
						int val = (o2.x - o.x) * 10;
						ans = Math.min(ans, val);
					}
				}
			}
		}

		// 上右
		Map<Integer, TreeSet<Obj>> mapU = make(arr, "U", true);
		Map<Integer, TreeSet<Obj>> mapR = make(arr, "R", true);
		for (Integer v : mapU.keySet()) {
			TreeSet<Obj> set = mapU.get(v);
			TreeSet<Obj> set2 = mapR.get(v);
			if (set2 != null) {
				for (Obj o : set) {
					Obj o2 = set2.lower(o);
					if (o2 != null) {
						int val = (o.x - o2.x) * 10;
						ans = Math.min(ans, val);
					}
				}
			}
		}

		// 下右
		mapD = make(arr, "D", false);
		mapR = make(arr, "R", false);
		for (Integer v : mapD.keySet()) {
			TreeSet<Obj> set = mapD.get(v);
			TreeSet<Obj> set2 = mapR.get(v);
			if (set2 != null) {
				for (Obj o : set) {
					Obj o2 = set2.lower(o);
					if (o2 != null) {
						int val = (o.x - o2.x) * 10;
						ans = Math.min(ans, val);
					}
				}
			}
		}

		// 上左
		mapU = make(arr, "U", false);
		mapL = make(arr, "L", false);
		for (Integer v : mapU.keySet()) {
			TreeSet<Obj> set = mapU.get(v);
			TreeSet<Obj> set2 = mapL.get(v);
			if (set2 != null) {
				for (Obj o : set) {
					Obj o2 = set2.higher(o);
					if (o2 != null) {
						int val = (o2.x - o.x) * 10;
						ans = Math.min(ans, val);
					}
				}
			}
		}

		if (ans == Integer.MAX_VALUE) {
			System.out.println("SAFE");
		} else {
			System.out.println(ans);
		}
	}

	static Map<Integer, TreeSet<Obj>> make(Obj[] arr, String u, boolean plus) {
		Map<Integer, TreeSet<Obj>> map = new HashMap<>();
		for (Obj o : arr) {
			if (u.equals(o.u)) {
				if (plus) {
					o.val = o.y + o.x;
				} else {
					o.val = o.y - o.x;
				}
				TreeSet<Obj> set = map.get(o.val);
				if (set == null) {
					 set = new TreeSet<Obj>((o1, o2) -> o1.x - o2.x);
					 map.put(o.val, set);
				}
				set.add(o);
			}
		}
		return map;
	}

	static class Obj {
		int x, y, val;
		String u;
	}
}