(eval-when (:compile-toplevel :load-toplevel :execute)
  (sb-int:defconstant-eqx opt
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0))
    #'equal)
  #+swank (ql:quickload '(:cl-debug-print :fiveam) :silent t)
  #-swank (set-dispatch-macro-character
           #\# #\> (lambda (s c p) (declare (ignore c p)) `(values ,(read s nil nil t)))))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
(define-int-types 2 4 7 8 15 16 31 32 62 63 64)

;; BEGIN_INSERTED_CONTENTS
(macrolet ((def (name fname)
             `(define-modify-macro ,name (new-value) ,fname)))
  (def minf min)
  (def maxf max)
  (def mulf *)
  (def divf /)
  (def iorf logior)
  (def xorf logxor)
  (def andf logand))

(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  "NOTE: cannot read -2^62"
  (macrolet ((%read-byte ()
               `(the (unsigned-byte 8)
                     #+swank (char-code (read-char in nil #\Nul))
                     #-swank (sb-impl::ansi-stream-read-byte in nil #.(char-code #\Nul) nil))))
    (let* ((minus nil)
           (result (loop (let ((byte (%read-byte)))
                           (cond ((<= 48 byte 57)
                                  (return (- byte 48)))
                                 ((zerop byte) ; #\Nul
                                  (error "Read EOF or #\Nul."))
                                 ((= byte #.(char-code #\-))
                                  (setq minus t)))))))
      (declare ((integer 0 #.most-positive-fixnum) result))
      (loop
        (let* ((byte (%read-byte)))
          (if (<= 48 byte 57)
              (setq result (+ (- byte 48)
                              (* 10 (the (integer 0 #.(floor most-positive-fixnum 10))
                                         result))))
              (return (if minus (- result) result))))))))

(in-package :cl-user)

(defmacro dbg (&rest forms)
  #+swank
  (if (= (length forms) 1)
      `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms))
      `(format *error-output* "~A => ~A~%" ',forms `(,,@forms)))
  #-swank (declare (ignore forms)))

(declaim (inline println))
(defun println (obj &optional (stream *standard-output*))
  (let ((*read-default-float-format* 'double-float))
    (prog1 (princ obj stream) (terpri stream))))

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;

(defconstant +inf+ most-positive-fixnum)
(defun solve-horizontal (lxs lys rxs rys)
  (declare #.opt
           (inline sort)
           ((array uint31 (*)) lxs lys rxs rys))
  (let ((ltable (make-array 200001))
        (rtable (make-array 200001))
        (res +inf+))
    (declare (uint62 res))
    (dotimes (i (length ltable))
      (setf (aref ltable i) (make-array 0 :element-type 'uint31 :fill-pointer 0)))
    (dotimes (i (length rtable))
      (setf (aref rtable i) (make-array 0 :element-type 'uint31 :fill-pointer 0)))
    (loop for x across lxs
          for y across lys
          do (vector-push-extend x (aref ltable y))
             (aref ltable y))
    (loop for x across rxs
          for y across rys
          do (vector-push-extend x (aref rtable y)))
    (dotimes (i (length ltable))
      (setf (aref ltable i) (sort (the (array uint31 (*)) (aref ltable i)) #'<)
            (aref rtable i) (sort (the (array uint31 (*)) (aref rtable i)) #'<)))
    (loop for lvec of-type (array uint31 (*)) across ltable
          for rvec of-type (array uint31 (*)) across rtable
          for lpos = 0
          do (loop for rpos from 0 below (length rvec)
                   for rx = (aref rvec rpos)
                   do (loop (when (= lpos (length lvec))
                              (return))
                            (let ((lx (aref lvec lpos)))
                              (when (<= rx lx)
                                (return)))
                            (incf lpos))
                      (when (= lpos (length lvec))
                        (return))
                      (let ((lx (aref lvec lpos)))
                        (minf res (- lx rx)))))
    (* res 5)))

(defun solve-ru (rxs rys uxs uys)
  (declare #.opt
           (inline sort)
           ((array uint31 (*)) rxs rys uxs uys))
  (let ((rtable (make-array 400001))
        (utable (make-array 400001))
        (res +inf+))
    (declare (uint62 res))
    (dotimes (i (length rtable))
      (setf (aref rtable i) (make-array 0 :element-type 'uint31 :fill-pointer 0)))
    (dotimes (i (length utable))
      (setf (aref utable i) (make-array 0 :element-type 'uint31 :fill-pointer 0)))
    (loop for x across rxs
          for y across rys
          for b = (+ x y)
          do (vector-push-extend x (aref rtable b))
             (aref rtable y))
    (loop for x across uxs
          for y across uys
          for b = (+ x y)
          do (vector-push-extend x (aref utable b)))
    (dotimes (i (length rtable))
      (setf (aref rtable i) (sort (the (array uint31 (*)) (aref rtable i)) #'<)
            (aref utable i) (sort (the (array uint31 (*)) (aref utable i)) #'<)))
    (loop for rvec of-type (array uint31 (*)) across rtable
          for uvec of-type (array uint31 (*)) across utable
          for upos = 0
          do (loop for rpos from 0 below (length rvec)
                   for rx = (aref rvec rpos)
                   do (loop (when (= upos (length uvec))
                              (return))
                            (let ((ux (aref uvec upos)))
                              (when (<= rx ux)
                                (return)))
                            (incf upos))
                      (when (= upos (length uvec))
                        (return))
                      (let ((ux (aref uvec upos)))
                        (minf res (- ux rx)))))
    (* res 10)))
(defun main ()
  (let* ((n (read))
         (lxs (make-array n :element-type 'uint31 :fill-pointer 0))
         (lys (make-array n :element-type 'uint31 :fill-pointer 0))
         (rxs (make-array n :element-type 'uint31 :fill-pointer 0))
         (rys (make-array n :element-type 'uint31 :fill-pointer 0))
         (uxs (make-array n :element-type 'uint31 :fill-pointer 0))
         (uys (make-array n :element-type 'uint31 :fill-pointer 0))
         (dxs (make-array n :element-type 'uint31 :fill-pointer 0))
         (dys (make-array n :element-type 'uint31 :fill-pointer 0)))
    (dotimes (i n)
      (let ((x (read-fixnum))
            (y (read-fixnum))
            (u (read-char)))
        (ecase u
          (#\U (vector-push x uxs)
           (vector-push y uys))
          (#\D (vector-push x dxs)
           (vector-push y dys))
          (#\L (vector-push x lxs)
           (vector-push y lys))
          (#\R (vector-push x rxs)
           (vector-push y rys)))))
    (labels ((inv! (vec)
               (declare ((array uint31 (*)) vec))
               (dotimes (i (length vec))
                 (setf (aref vec i) (- 200000 (aref vec i))))))
      (let ((res (min (solve-horizontal lxs lys rxs rys)
                      (solve-horizontal dys dxs uys uxs))))
        (minf res (solve-ru rxs rys uxs uys))
        #>res
        (inv! lxs) (inv! uxs) ;; X座標について反転してL->R
        (minf res (solve-ru lxs lys uxs uys))
        (inv! lxs) (inv! uxs)
        (inv! rys) (inv! dys) ;; Y座標について反転してD->U
        (minf res (solve-ru rxs rys dxs dys))
        (inv! rys) (inv! dys)
        (inv! lxs) (inv! lys) (inv! dxs) (inv! dys)
        (minf res (solve-ru lxs lys dxs dys))
        (inv! lxs) (inv! lys) (inv! dxs) (inv! dys)
        (println
         (if (>= res +inf+)
             "SAFE"
             res))))))

;; 168 224 U
;; 130 175 R
;; 111 198 D
;; 121 188 L
;; 201 116 U
;; 112 121 R
;; 145 239 D
;; 185 107 L

#-swank (main)

;;;
;;; Test and benchmark
;;;

#+swank
(defun io-equal (in-string out-string &key (function #'main) (test #'equal))
  "Passes IN-STRING to *STANDARD-INPUT*, executes FUNCTION, and returns true if
the string output to *STANDARD-OUTPUT* is equal to OUT-STRING."
  (labels ((ensure-last-lf (s)
             (if (eql (uiop:last-char s) #\Linefeed)
                 s
                 (uiop:strcat s uiop:+lf+))))
    (funcall test
             (ensure-last-lf out-string)
             (with-output-to-string (out)
               (let ((*standard-output* out))
                 (with-input-from-string (*standard-input* (ensure-last-lf in-string))
                   (funcall function)))))))

#+swank
(defun get-clipbrd ()
  (with-output-to-string (out)
    #+os-windows (run-program "powershell.exe" '("-Command" "Get-Clipboard") :output out :search t)
    #+os-unix (run-program "xsel" '("-b" "-o") :output out :search t)))

#+swank (defparameter *this-pathname* (uiop:current-lisp-file-pathname))
#+swank (defparameter *dat-pathname* (uiop:merge-pathnames* "test.dat" *this-pathname*))

#+swank
(defun run (&optional thing (out *standard-output*))
  "THING := null | string | symbol | pathname

null: run #'MAIN using the text on clipboard as input.
string: run #'MAIN using the string as input.
symbol: alias of FIVEAM:RUN!.
pathname: run #'MAIN using the text file as input."
  (let ((*standard-output* out))
    (etypecase thing
      (null
       (with-input-from-string (*standard-input* (delete #\Return (get-clipbrd)))
         (main)))
      (string
       (with-input-from-string (*standard-input* (delete #\Return thing))
         (main)))
      (symbol (5am:run! thing))
      (pathname
       (with-open-file (*standard-input* thing)
         (main))))))

#+swank
(defun gen-dat ()
  (uiop:with-output-file (out *dat-pathname* :if-exists :supersede)
    (format out "")))

#+swank
(defun bench (&optional (out (make-broadcast-stream)))
  (time (run *dat-pathname* out)))

;; To run: (5am:run! :sample)
#+swank
(it.bese.fiveam:test :sample
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "3
1 2 300
3 3 600
1 4 800
"
    "2900
900
0
0
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "5
3 5 400
5 3 700
5 5 1000
5 7 700
7 5 400
"
    "13800
1600
0
0
0
0
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "6
2 5 1000
5 2 1100
5 5 1700
-2 -5 900
-5 -2 600
-5 -5 2200
"
    "26700
13900
3200
1200
0
0
0
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "8
2 2 286017
3 1 262355
2 -2 213815
1 -3 224435
-2 -2 136860
-3 -1 239338
-2 2 217647
-1 3 141903
"
    "2576709
1569381
868031
605676
366338
141903
0
0
0
")))


Measured memory_bytes: 444488