program main
    implicit none
    integer(8) n
    integer(8), allocatable, dimension(:) :: x,y
    character(:), allocatable :: u(:)
    integer(8) i, j, c
    integer dx, dy, dt, dt_min
    read(*,*) n
    allocate(x(n))
    allocate(y(n))
    allocate(character(1) :: u(n))
    do i = 1, n
        read(*,*) x(i), y(i), u(i)
    end do

    dt_min = 400000
    dt = dt_min
    c = 0

    do i = 1, n
        do j = i+1, n
            dx = x(j) - x(i)
            dy = y(j) - y(i)
            if(u(j) == "U") then
                if(dy < 0) then
                    if(dx < 0) then
                        if(u(i) == "L") then
                            if(abs(dx) == abs(dy)) then
                                dt = abs(dy)*10
                                c = c + 1
                            end if
                        end if
                    else if(dx == 0) then
                        if(u(i) == "D") then
                            dt = abs(dy)*5
                            c = c + 1
                        end if
                    else if(dx > 0) then
                        if(u(i) == "R") then
                            if(abs(dx) == abs(dy)) then
                                dt = abs(dy)*10
                                c = c + 1
                            end if
                        end if
                    end if
                end if
            else if(u(j) == "D") then
                if(dy > 0) then
                    if(dx < 0) then
                        if(u(i) == "L") then
                            if(abs(dx) == abs(dy)) then
                                dt = abs(dy)*10
                                c = c + 1
                            end if
                        end if
                    else if(dx == 0) then
                        if(u(i) == "U") then
                            dt = abs(dy)*5
                            c = c + 1
                        end if
                    else if(dx > 0) then
                        if(u(i) == "R") then
                            if(abs(dx) == abs(dy)) then
                                dt = abs(dy)*10
                                c = c + 1
                            end if
                        end if
                    end if
                end if
            else if(u(j) == "R") then
                if(dx < 0) then
                    if(dy < 0) then
                        if(u(i) == "D") then
                            if(abs(dx) == abs(dy)) then
                                dt = abs(dy)*10
                                c = c + 1
                            end if
                        end if
                    else if(dy == 0) then
                        if(u(i) == "L") then
                            dt = abs(dx)*5
                            c = c + 1
                        end if
                    else if(dy > 0) then
                        if(u(i) == "U") then
                            if(abs(dx) == abs(dy)) then
                                dt = abs(dy)*10
                                c = c + 1
                            end if
                        end if
                    end if
                end if
            else if(u(j) == "L") then
                if(dx > 0) then
                    if(dy < 0) then
                        if(u(i) == "D") then
                            if(abs(dx) == abs(dy)) then
                                dt = abs(dy)*10
                                c = c + 1
                            end if
                        end if
                    else if(dy == 0) then
                        if(u(i) == "R") then
                            dt = abs(dx)*5
                            c = c + 1
                        end if
                    else if(dy > 0) then
                        if(u(i) == "U") then
                            if(abs(dx) == abs(dy)) then
                                dt = abs(dy)*10
                                c = c + 1
                            end if
                        end if
                    end if
                end if
            end if
            if (dt < dt_min) then
                dt_min = dt
            end if
        end do
    end do
    
    if (c == 0) then
        write(*,*) "SAFE"
    else
        write(*,*) dt_min
    end if

end program
