use std::cmp;
use std::collections::HashMap;

fn main() {
    let n: usize = {
        let mut buf = String::new();
        std::io::stdin().read_line(&mut buf).unwrap();
        buf.trim_end().parse().unwrap()
    };
    let mut x: Vec<i32> = vec![0; n];
    let mut y: Vec<i32> = vec![0; n];
    let mut u: Vec<char> = vec!['\0'; n];
    for i in 0..n {
        let mut buf = String::new();
        std::io::stdin().read_line(&mut buf).unwrap();
        let mut iter = buf.split_whitespace();
        x[i] = iter.next().unwrap().parse().unwrap();
        y[i] = iter.next().unwrap().parse().unwrap();
        u[i] = iter.next().unwrap().parse().unwrap();
    }

    let mut ans = std::i32::MAX;
    {
        let mut map = HashMap::new();
        for i in 0..n {
            if u[i] == 'U' || u[i] == 'D' {
                (*map.entry(x[i]).or_insert(Vec::new())).push(i);
            }
        }
        for v in map.values_mut() {
            v.sort_by(|&i, &j| y[i].cmp(&y[j]));
            for k in 0..(v.len() - 1) {
                let (i, j) = (v[k], v[k + 1]);
                if u[i] == 'U' && u[j] == 'D' {
                    ans = cmp::min(ans, (y[j] - y[i]) * 5);
                }
            }
        }
    }
    {
        let mut map = HashMap::new();
        for i in 0..n {
            if u[i] == 'R' || u[i] == 'L' {
                (*map.entry(y[i]).or_insert(Vec::new())).push(i);
            }
        }
        for v in map.values_mut() {
            v.sort_by(|&i, &j| x[i].cmp(&x[j]));
            for k in 0..(v.len() - 1) {
                let (i, j) = (v[k], v[k + 1]);
                if u[i] == 'R' && u[j] == 'L' {
                    ans = cmp::min(ans, (x[j] - x[i]) * 5);
                }
            }
        }
    }
    {
        let mut map = HashMap::new();
        for i in 0..n {
            if u[i] == 'U' || u[i] == 'R' {
                (*map.entry(x[i] + y[i]).or_insert(Vec::new())).push(i);
            }
        }
        for v in map.values_mut() {
            v.sort_by(|&i, &j| y[i].cmp(&y[j]));
            for k in 0..(v.len() - 1) {
                let (i, j) = (v[k], v[k + 1]);
                if u[i] == 'U' && u[j] == 'R' {
                    ans = cmp::min(ans, (y[j] - y[i]) * 10);
                }
            }
        }
    }
    {
        let mut map = HashMap::new();
        for i in 0..n {
            if u[i] == 'R' || u[i] == 'D' {
                (*map.entry(x[i] - y[i]).or_insert(Vec::new())).push(i);
            }
        }
        for v in map.values_mut() {
            v.sort_by(|&i, &j| x[i].cmp(&x[j]));
            for k in 0..(v.len() - 1) {
                let (i, j) = (v[k], v[k + 1]);
                if u[i] == 'R' && u[j] == 'D' {
                    ans = cmp::min(ans, (x[j] - x[i]) * 10);
                }
            }
        }
    }
    {
        let mut map = HashMap::new();
        for i in 0..n {
            if u[i] == 'D' || u[i] == 'L' {
                (*map.entry(x[i] + y[i]).or_insert(Vec::new())).push(i);
            }
        }
        for v in map.values_mut() {
            v.sort_by(|&i, &j| y[j].cmp(&y[i]));
            for k in 0..(v.len() - 1) {
                let (i, j) = (v[k], v[k + 1]);
                if u[i] == 'D' && u[j] == 'L' {
                    ans = cmp::min(ans, (y[i] - y[j]) * 10);
                }
            }
        }
    }
    {
        let mut map = HashMap::new();
        for i in 0..n {
            if u[i] == 'L' || u[i] == 'U' {
                (*map.entry(x[i] - y[i]).or_insert(Vec::new())).push(i);
            }
        }
        for v in map.values_mut() {
            v.sort_by(|&i, &j| x[j].cmp(&x[i]));
            for k in 0..(v.len() - 1) {
                let (i, j) = (v[k], v[k + 1]);
                if u[i] == 'L' && u[j] == 'U' {
                    ans = cmp::min(ans, (x[i] - x[j]) * 10);
                }
            }
        }
    }
    
    if ans == std::i32::MAX {
        println!("SAFE");
    } else {
        println!("{}", ans);
    }
}

Measured memory_bytes: 24824