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: Wrong Answer)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.TreeSet;
import java.util.ArrayList;
import java.io.BufferedOutputStream;
import java.nio.charset.Charset;
import java.util.StringTokenizer;
import java.util.Map;
import java.io.OutputStreamWriter;
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.util.List;
import java.io.Writer;
import java.io.BufferedReader;
import java.util.Comparator;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author mikit
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        LightScanner in = new LightScanner(inputStream);
        LightWriter out = new LightWriter(outputStream);
        FAirSafety solver = new FAirSafety();
        solver.solve(1, in, out);
        out.close();
    }

    static class FAirSafety {
        public void solve(int testNumber, LightScanner in, LightWriter out) {
            int n = in.ints();
            Map<Integer, List<FAirSafety.P>> upd = new HashMap<>(), lr = new HashMap<>();
            List<FAirSafety.P> right = new ArrayList<>(), left = new ArrayList<>();
            Map<Integer, TreeSet<Integer>> upR = new HashMap<>();
            Map<Integer, TreeSet<Integer>> upL = new HashMap<>();
            Map<Integer, TreeSet<Integer>> downR = new HashMap<>();
            Map<Integer, TreeSet<Integer>> downL = new HashMap<>();
            for (int i = 0; i < n; i++) {
                int x = in.ints(), y = in.ints();
                char c = in.chr();
                FAirSafety.P p = new FAirSafety.P(x, y, c);
                if (c == 'R') {
                    right.add(p);
                    lr.computeIfAbsent(p.y, k -> new ArrayList<>()).add(p);
                }
                if (c == 'L') {
                    left.add(p);
                    lr.computeIfAbsent(p.y, k -> new ArrayList<>()).add(p);
                }
                if (c == 'U') {
                    TreeSet<Integer> r = upR.get(x + y);
                    if (r == null) r = new TreeSet<>();
                    r.add(x);
                    upR.put(x + y, r);

                    TreeSet<Integer> l = upL.get(x - y);
                    if (l == null) l = new TreeSet<>();
                    l.add(x);
                    upL.put(x - y, r);

                    upd.computeIfAbsent(p.x, k -> new ArrayList<>()).add(p);
                }
                if (c == 'D') {
                    TreeSet<Integer> r = downR.get(x - y);
                    if (r == null) r = new TreeSet<>();
                    r.add(x);
                    downR.put(x - y, r);

                    TreeSet<Integer> l = downL.get(x + y);
                    if (l == null) l = new TreeSet<>();
                    l.add(x);
                    downL.put(x + y, r);

                    upd.computeIfAbsent(p.x, k -> new ArrayList<>()).add(p);
                }
            }

            long ans = Integer.MAX_VALUE;

            for (List<FAirSafety.P> li : upd.values()) {
                li.sort(Comparator.comparing(p -> p.y));
                for (int i = 1; i < li.size(); i++) {
                    if (li.get(i - 1).d == 'U' && li.get(i).d == 'D') {
                        ans = Math.min(ans, 5 * (li.get(i).y - li.get(i - 1).y));
                    }
                }
            }
            for (List<FAirSafety.P> li : lr.values()) {
                li.sort(Comparator.comparing(p -> p.x));
                for (int i = 1; i < li.size(); i++) {
                    if (li.get(i - 1).d == 'R' && li.get(i).d == 'L') {
                        ans = Math.min(ans, 5 * (li.get(i).x - li.get(i - 1).x));
                    }
                }
            }


            for (FAirSafety.P p : right) {
                TreeSet<Integer> up = upR.get(p.x + p.y);
                if (up != null) {
                    //System.out.println(p);
                    Integer t = up.ceiling(p.x);
                    if (t != null) ans = Math.min(ans, (t - p.x) * 10);
                }
                TreeSet<Integer> down = downR.get(p.x - p.y);
                if (down != null) {
                    //System.out.println(p);
                    Integer t = down.ceiling(p.x);
                    if (t != null) ans = Math.min(ans, (t - p.x) * 10);
                }
            }

            for (FAirSafety.P p : left) {
                TreeSet<Integer> up = upL.get(p.x - p.y);
                if (up != null) {
                    //System.out.println(p);
                    Integer t = up.floor(p.x);
                    if (t != null) ans = Math.min(ans, (p.x - t) * 10);
                }
                TreeSet<Integer> down = downL.get(p.x + p.y);
                if (down != null) {
                    //System.out.println(p);
                    Integer t = down.floor(p.x);
                    if (t != null) ans = Math.min(ans, (p.x - t) * 10);
                }
            }

            if (ans < 0) throw new RuntimeException();
            if (ans >= Integer.MAX_VALUE) out.ans("SAFE").ln();
            else out.ans(ans).ln();
        }

        private static class P {
            int x;
            int y;
            char d;

            P(int x, int y) {
                this.x = x;
                this.y = y;
            }

            public P(int x, int y, char d) {
                this.x = x;
                this.y = y;
                this.d = d;
            }

            public String toString() {
                return "P{" +
                        "x=" + x +
                        ", y=" + y +
                        '}';
            }

        }

    }

    static class LightWriter implements AutoCloseable {
        private final Writer out;
        private boolean autoflush = false;
        private boolean breaked = true;

        public LightWriter(Writer out) {
            this.out = out;
        }

        public LightWriter(OutputStream out) {
            this(new OutputStreamWriter(new BufferedOutputStream(out), Charset.defaultCharset()));
        }

        public LightWriter print(char c) {
            try {
                out.write(c);
                breaked = false;
            } catch (IOException ex) {
                throw new UncheckedIOException(ex);
            }
            return this;
        }

        public LightWriter print(String s) {
            try {
                out.write(s, 0, s.length());
                breaked = false;
            } catch (IOException ex) {
                throw new UncheckedIOException(ex);
            }
            return this;
        }

        public LightWriter ans(String s) {
            if (!breaked) {
                print(' ');
            }
            return print(s);
        }

        public LightWriter ans(long l) {
            return ans(Long.toString(l));
        }

        public LightWriter ln() {
            print(System.lineSeparator());
            breaked = true;
            if (autoflush) {
                try {
                    out.flush();
                } catch (IOException ex) {
                    throw new UncheckedIOException(ex);
                }
            }
            return this;
        }

        public void close() {
            try {
                out.close();
            } catch (IOException ex) {
                throw new UncheckedIOException(ex);
            }
        }

    }

    static class LightScanner implements AutoCloseable {
        private BufferedReader reader = null;
        private StringTokenizer tokenizer = null;

        public LightScanner(InputStream in) {
            reader = new BufferedReader(new InputStreamReader(in));
        }

        public String string() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new UncheckedIOException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public char chr() {
            return string().charAt(0);
        }

        public int ints() {
            return Integer.parseInt(string());
        }

        public void close() {
            try {
                this.reader.close();
            } catch (IOException ex) {
                throw new UncheckedIOException(ex);
            }
        }

    }
}