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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.ArrayList;
import java.io.BufferedOutputStream;
import java.util.OptionalLong;
import java.nio.charset.Charset;
import java.util.StringTokenizer;
import java.util.Map;
import java.io.OutputStreamWriter;
import java.io.OutputStream;
import java.util.stream.LongStream;
import java.util.Collection;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.util.List;
import java.util.stream.Stream;
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>> lr = new HashMap<>();
            Map<Integer, List<FAirSafety.P>> du = new HashMap<>();
            Map<Integer, List<FAirSafety.P>> ul = new HashMap<>();
            Map<Integer, List<FAirSafety.P>> ur = new HashMap<>();
            Map<Integer, List<FAirSafety.P>> dl = new HashMap<>();
            Map<Integer, List<FAirSafety.P>> dr = new HashMap<>();
            for (int i = 0; i < n; i++) {
                int x = in.ints(), y = in.ints();
                char c = in.chr();
                if (c == 'D') {
                    du.computeIfAbsent(x, k -> new ArrayList<>()).add(new FAirSafety.P(y, -1));
                    dl.computeIfAbsent(x + y, k -> new ArrayList<>()).add(new FAirSafety.P(x, 1));
                    dr.computeIfAbsent(x - y, k -> new ArrayList<>()).add(new FAirSafety.P(x, -1));
                }
                if (c == 'U') {
                    du.computeIfAbsent(x, k -> new ArrayList<>()).add(new FAirSafety.P(y, 1));
                    ul.computeIfAbsent(x - y, k -> new ArrayList<>()).add(new FAirSafety.P(x, 1));
                    ur.computeIfAbsent(x + y, k -> new ArrayList<>()).add(new FAirSafety.P(x, -1));
                }
                if (c == 'L') {
                    lr.computeIfAbsent(y, k -> new ArrayList<>()).add(new FAirSafety.P(x, -1));
                    dl.computeIfAbsent(x + y, k -> new ArrayList<>()).add(new FAirSafety.P(x, -1));
                    ul.computeIfAbsent(x - y, k -> new ArrayList<>()).add(new FAirSafety.P(x, -1));
                }
                if (c == 'R') {
                    lr.computeIfAbsent(y, k -> new ArrayList<>()).add(new FAirSafety.P(x, 1));
                    dr.computeIfAbsent(x - y, k -> new ArrayList<>()).add(new FAirSafety.P(x, 1));
                    ur.computeIfAbsent(x + y, k -> new ArrayList<>()).add(new FAirSafety.P(x, 1));
                }
            }

            long ans = Integer.MAX_VALUE;
            ans = Math.min(ans, 5 * lr.values().stream().map(FAirSafety::solve).mapToLong(x -> x).min().orElse(Integer.MAX_VALUE));
            ans = Math.min(ans, 5 * du.values().stream().map(FAirSafety::solve).mapToLong(x -> x).min().orElse(Integer.MAX_VALUE));
            ans = Math.min(ans, 10 * dr.values().stream().map(FAirSafety::solve).mapToLong(x -> x).min().orElse(Integer.MAX_VALUE));
            ans = Math.min(ans, 10 * dl.values().stream().map(FAirSafety::solve).mapToLong(x -> x).min().orElse(Integer.MAX_VALUE));
            ans = Math.min(ans, 10 * ur.values().stream().map(FAirSafety::solve).mapToLong(x -> x).min().orElse(Integer.MAX_VALUE));
            ans = Math.min(ans, 10 * ul.values().stream().map(FAirSafety::solve).mapToLong(x -> x).min().orElse(Integer.MAX_VALUE));


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

        private static long solve(List<FAirSafety.P> li) {
            long ans = Integer.MAX_VALUE;
            li.sort(Comparator.comparing(p -> p.x));
            for (int i = 1; i < li.size(); i++) {
                if (li.get(i - 1).d == 1 && li.get(i).d == -1) {
                    ans = Math.min(ans, li.get(i).x - li.get(i - 1).x);
                }
            }
            return ans;
        }

        private static class P {
            int x;
            int d;

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

        }

    }

    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);
            }
        }

    }
}

