// -*- coding:utf-8-unix -*-

use proconio::{fastout, input};

// ABC086C - Traveling
// https://atcoder.jp/contests/abs/fasks/arc089_a

#[fastout]
fn main() {
    input! {
        n: usize,
        xyu: [(isize, isize, char); n],
    }
    let mut xyu: Vec<(isize, isize, char)> = xyu.clone();

    let mut collision_time = 99999999;
    // UD collision check
    xyu.sort_unstable_by_key(|&(_x, y, _u)| y);
    xyu.sort_by_key(|&(x, _y, _u)| x);
    let mut nowx = 0;
    let mut prevy: Option<isize> = None;
    for &(x, y, u) in &xyu {
        if nowx != x {
            nowx = x;
            prevy = None;
        }
        if u == 'U' {
            prevy = Some(y);
        } else if u == 'D' {
            match prevy {
                None => {}
                Some(yy) => {
                    let coltime = (y - yy) * 5;
                    collision_time = collision_time.min(coltime);
                }
            }
            prevy = None;
        }
    }
    // RL collision check
    xyu.sort_unstable_by_key(|&(x, _y, _u)| x);
    xyu.sort_by_key(|&(_x, y, _u)| y);
    let mut nowy = 0;
    let mut prevx: Option<isize> = None;
    for &(x, y, u) in &xyu {
        if nowy != y {
            nowy = y;
            prevx = None;
        }
        if u == 'R' {
            prevx = Some(x);
        } else if u == 'L' {
            match prevx {
                None => {}
                Some(xx) => {
                    let coltime = (x - xx) * 5;
                    collision_time = collision_time.min(coltime);
                }
            }
            prevx = None;
        }
    }
    // UL collision check
    xyu.sort_unstable_by_key(|&(x, y, _)| x + y);
    xyu.sort_by_key(|&(x, y, _)| x - y);
    let mut noww = 0;
    let mut prevv: Option<isize> = None;
    for &(x, y, u) in &xyu {
        if noww != x - y {
            noww = x - y;
            prevv = None;
        }
        if u == 'U' {
            prevv = Some(x + y);
        } else if u == 'L' {
            match prevv {
                None => {}
                Some(vv) => {
                    let coltime = (x + y - vv) * 5;
                    collision_time = collision_time.min(coltime);
                }
            }
            prevv = None;
        }
    }
    // RU collision check
    xyu.sort_unstable_by_key(|&(x, y, _)| x - y);
    xyu.sort_by_key(|&(x, y, _)| x + y);
    let mut nowv = 0;
    let mut prevw: Option<isize> = None;
    for &(x, y, u) in &xyu {
        if nowv != x + y {
            nowv = x + y;
            prevw = None;
        }
        if u == 'R' {
            prevw = Some(x - y);
        } else if u == 'U' {
            match prevw {
                None => {}
                Some(ww) => {
                    let coltime = (x - y - ww) * 5;
                    collision_time = collision_time.min(coltime);
                }
            }
            prevw = None;
        }
    }
    // RD collision check
    xyu.sort_unstable_by_key(|&(x, y, _)| x + y);
    xyu.sort_by_key(|&(x, y, _)| x - y);
    let mut noww = 0;
    let mut prevv: Option<isize> = None;
    for &(x, y, u) in &xyu {
        if noww != x - y {
            noww = x - y;
            prevv = None;
        }
        if u == 'R' {
            prevv = Some(x + y);
        } else if u == 'D' {
            match prevv {
                None => {}
                Some(vv) => {
                    let coltime = (x + y - vv) * 5;
                    collision_time = collision_time.min(coltime);
                }
            }
            prevv = None;
        }
    }
    // DL collision check
    xyu.sort_unstable_by_key(|&(x, y, _)| x - y);
    xyu.sort_by_key(|&(x, y, _)| x + y);
    let mut nowv = 0;
    let mut prevw: Option<isize> = None;
    for &(x, y, u) in &xyu {
        if nowv != x + y {
            nowv = x + y;
            prevw = None;
        }
        if u == 'D' {
            prevw = Some(x - y);
        } else if u == 'L' {
            match prevw {
                None => {}
                Some(ww) => {
                    let coltime = (x - y - ww) * 5;
                    collision_time = collision_time.min(coltime);
                }
            }
            prevw = None;
        }
    }
    if collision_time < 10000000 {
        println!("{}", collision_time);
    } else {
        println!("SAFE");
    }
}


Measured memory_bytes: 16412