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 Rust (1.42.0) (judge verdict: Runtime Error)

fn main() {
    proconio::input! {
        n: usize,
        xyus: [(i64, i64, char); n],
    }
    let (mut us, mut rs, mut ds, mut ls) = (vec![], vec![], vec![], vec![]);
    for (x, y, u) in xyus {
        let v = match u {
            'U' => &mut us,
            'R' => &mut rs,
            'D' => &mut ds,
            'L' => &mut ls,
            _ => unreachable!(),
        };
        v.push((x, y));
    }
    us.sort_by_key(|(x, _y)| *x);
    ds.sort_by_key(|(x, _y)| *x);
    rs.sort_by_key(|(_x, y)| *y);
    ls.sort_by_key(|(_x, y)| *y);
    let (us, rs, ds, ls) = (us, rs, ds, ls);
    fn fud(us: &[(i64, i64)], ds: &[(i64, i64)]) -> Option<i64> {
        let mut m = std::collections::HashMap::new();
        for (x, y) in us {
            m.entry(x)
                .or_insert((
                    std::collections::BTreeSet::new(),
                    std::collections::BTreeSet::new(),
                ))
                .0
                .insert(y);
        }
        for (x, y) in ds {
            m.entry(x)
                .or_insert((
                    std::collections::BTreeSet::new(),
                    std::collections::BTreeSet::new(),
                ))
                .1
                .insert(y);
        }
        let mut udmin = None;
        for (us, ds) in m.values() {
            if us.is_empty() || ds.is_empty() {
                continue;
            } else if us.iter().next().unwrap() > ds.iter().rev().next().unwrap() {
                continue;
            } else {
                let v = us
                    .iter()
                    .map(|y| (y, 'U'))
                    .chain(ds.iter().map(|y| (y, 'D')))
                    .collect::<Vec<_>>();
                for w in v.windows(2) {
                    if w[0].1 == 'U' && w[1].1 == 'D' {
                        let t = (**w[1].0 - **w[0].0) * 10 / 2;
                        if udmin.is_none() {
                            udmin = Some(t);
                        } else {
                            udmin = Some(std::cmp::min(t, udmin.unwrap()));
                        }
                    }
                }
            }
        }
        udmin
    }
    let udmin = fud(&us, &ds);
    let rlmin = fud(
        &rs.iter().cloned().map(|(x, y)| (y, x)).collect::<Vec<_>>(),
        &ls.iter().cloned().map(|(x, y)| (y, x)).collect::<Vec<_>>(),
    );
    fn fur(us: &[(i64, i64)], rs: &[(i64, i64)]) -> Option<i64> {
        let mut m = std::collections::BTreeMap::new();
        for (x, y) in us {
            m.entry(x + y)
                .or_insert(std::collections::BTreeSet::new())
                .insert((x, y, 'U'));
        }
        for (x, y) in rs {
            m.entry(x + y)
                .or_insert(std::collections::BTreeSet::new())
                .insert((x, y, 'R'));
        }
        m.iter()
            .map(|(_key, value)| {
                value
                    .iter()
                    .collect::<Vec<_>>()
                    .windows(2)
                    .filter(|w| w[0].2 == 'R' && w[1].2 == 'U')
                    .map(|w| (w[0].0 - w[1].0).abs())
                    .min()
            })
            .filter(|x| x.is_some())
            .map(|x| x.unwrap() * 10)
            .min()
    }
    let urmin = fur(&us, &rs);
    let ulmin = fur(
        &us.iter().cloned().map(|(x, y)| (-x, y)).collect::<Vec<_>>(),
        &ls.iter().cloned().map(|(x, y)| (-x, y)).collect::<Vec<_>>(),
    );
    let drmin = fur(
        &ds.iter().cloned().map(|(x, y)| (x, -y)).collect::<Vec<_>>(),
        &rs.iter().cloned().map(|(x, y)| (x, -y)).collect::<Vec<_>>(),
    );
    let dlmin = fur(
        &ds.iter()
            .cloned()
            .map(|(x, y)| (-x, -y))
            .collect::<Vec<_>>(),
        &ls.iter()
            .cloned()
            .map(|(x, y)| (-x, -y))
            .collect::<Vec<_>>(),
    );
    if let Some((ans, b)) = vec![
        (udmin, true),
        (rlmin, true),
        (urmin, true),
        (ulmin, false),
        (drmin, false),
        (dlmin, true),
    ]
    .iter()
    .filter(|x| x.0.is_some())
    .map(|x| (x.0.unwrap(), x.1))
    .min()
    {
        if !b {
            unreachable!()
        }
        println!("{}", ans);
    } else {
        println!("SAFE");
    };
}