N = gets.to_i
U = []
R = []
D = []
L = []
N.times do 
    x,y,u = gets.split
    x = x.to_i
    y = y.to_i
    case u.chomp
        when "U" then U << [x,y]
        when "R" then R << [x,y]
        when "D" then D << [x,y]
        else L << [x,y]
    end
end

crash = nil

U.each do |u|
    D.each do |d|
        if (u[0] == d[0]) && (u[1] < d[1]) then
            t = (d[1] - u[1])*5
            crash = crash.nil? ? t : [crash, t].min
        end
    end
end

R.each do |r|
    L.each do |l|
        if (r[1] == l[1]) && (r[0] < l[0]) then
            t = (l[0] - r[0])*5
            crash = crash.nil? ? t : [crash, t].min
        end
    end
end

R.each do |r|
    U.each do |u|
        next if r[0] > u[0] || r[1] < u[1]
        if u[0] - r[0] == r[1] - u[1] then
            t = (u[0] - r[0]) * 10
            crash = crash.nil? ? t : [crash, t].min
        end
    end

    D.each do |d|
        next if r[0] > d[0] || r[1] > d[1]
        if d[0] - r[0] == d[1] - r[1] then
            t = (d[0] - r[0]) * 10
            crash = crash.nil? ? t : [crash, t].min
        end
    end
end

L.each do |l|
    U.each do |u|
        next if l[0] < u[0] || l[1] < u[1]
        if (u[0] - l[0]).abs == l[1] - u[1] then
            t = (u[0] - l[0]).abs * 10
            crash = crash.nil? ? t : [crash, t].min
        end
    end

    D.each do |d|
        next if l[0] < d[0] || l[1] > d[1]
        if (d[0] - l[0]).abs == d[1] - l[1] then
            t = (d[0] - l[0]).abs * 10
            crash = crash.nil? ? t : [crash, t].min
        end
    end
end

puts crash.nil? ? "SAFE" : crash