#![allow(unused_imports)]
#![allow(unused_macros)]
use std::cmp::{max, min};
use std::collections::*;
use std::io::{stdin, Read};

#[allow(unused_macros)]
macro_rules! parse {
    ($it: ident ) => {};
    ($it: ident, ) => {};
    ($it: ident, $var:ident : $t:tt $($r:tt)*) => {
        let $var = parse_val!($it, $t);
        parse!($it $($r)*);
    };
    ($it: ident, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = parse_val!($it, $t);
        parse!($it $($r)*);
    };
    ($it: ident, $var:ident $($r:tt)*) => {
        let $var = parse_val!($it, usize);
        parse!($it $($r)*);
    };
}

#[allow(unused_macros)]
macro_rules! parse_val {
    ($it: ident, [$t:tt; $len:expr]) => {
        (0..$len).map(|_| parse_val!($it, $t)).collect::<Vec<_>>();
    };
    ($it: ident, ($($t: tt),*)) => {
        ($(parse_val!($it, $t)),*)
    };
    ($it: ident, u1) => {
        $it.next().unwrap().parse::<usize>().unwrap() -1
    };
    ($it: ident, $t: ty) => {
        $it.next().unwrap().parse::<$t>().unwrap()
    };
}

#[cfg(debug_assertions)]
macro_rules! debug {
    ($( $args:expr ),*) => { eprintln!( $( $args ),* ); }
}

#[cfg(not(debug_assertions))]
macro_rules! debug {
    ($( $args:expr ),*) => {
        ()
    };
}

fn solve(s: &str) {
    let mut it = s.split_whitespace();
    parse!(it, n: usize);
    let mut a = BTreeMap::<i64, BTreeSet<(i64, char)>>::new();
    let mut b = BTreeMap::<i64, BTreeSet<(i64, char)>>::new();
    let mut c = BTreeMap::<i64, BTreeSet<(i64, char)>>::new();
    let mut d = BTreeMap::<i64, BTreeSet<(i64, char)>>::new();
    for _ in 0..n {
        parse!(it, x: i64, y: i64, u: char);
        a.entry(x - y).or_default().insert((x, u));
        b.entry(x + y).or_default().insert((x, u));
        c.entry(y).or_default().insert((x, u));
        d.entry(x).or_default().insert((y, u));
    }

    let mut ret = std::i64::MAX;
    let small = std::i64::MIN / 100;
    for set in a.values() {
        for &(c, t) in &[('U', 'L'), ('R', 'D')] {
            let mut prevx = small;
            for &(x, d) in set {
                if d == t {
                    ret = min(ret, (x - prevx) * 10);
                } else if d == c {
                    prevx = x;
                }
            }
        }
    }

    for set in b.values() {
        for &(c, t) in &[('R', 'U'), ('D', 'L')] {
            let mut prevx = small;
            for &(x, d) in set {
                if d == t {
                    ret = min(ret, (x - prevx) * 10);
                } else if d == c {
                    prevx = x;
                }
            }
        }
    }

    for set in c.values() {
        let (c, t) = ('R', 'L');
        let mut prevx = small;
        for &(x, d) in set {
            if d == t {
                ret = min(ret, (x - prevx) * 5);
            } else if d == c {
                prevx = x;
            }
        }
    }

    for set in d.values() {
        let (c, t) = ('U', 'D');
        let mut prevx = small;
        for &(x, d) in set {
            if d == t {
                ret = min(ret, (x - prevx) * 5);
            } else if d == c {
                prevx = x;
            }
        }
    }

    if ret > 20000000 {
        println!("{}", "SAFE");
    } else {
        assert!(ret > 0);
        println!("{}", ret);
    }
}

fn main() {
    let mut s = String::new();
    stdin().read_to_string(&mut s).unwrap();
    solve(&s);
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_input() {
        let s = "
        ";
        solve(s);
    }
}


Measured memory_bytes: 194044