(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
;;;
;;; Quicksort (deterministic median-of-three partitioning)
;;;

;; NOTE: This quicksort is NOT randomized. You should shuffle an input when you
;; need to avoid getting hacked.

;; Reference:
;; Hannu Erkio, The worst case permutation for median-of-three quicksort

(declaim (inline %median3))
(defun %median3 (x y z order)
  (if (funcall order x y)
      (if (funcall order y z)
          y
          (if (funcall order z x)
              x
              z))
      (if (funcall order z y)
          y
          (if (funcall order x z)
              x
              z))))

(declaim (inline quicksort!))
(defun quicksort! (vector order &key (start 0) end)
  "Destructively sorts VECTOR w.r.t. ORDER."
  (declare (vector vector))
  (unless end
    (setq end (length vector)))
  (assert (<= 0 start end))
  (labels
      ((recur (left right)
         (when (< left right)
           (let* ((l left)
                  (r right)
                  (pivot (%median3 (aref vector l)
                                   (aref vector (ash (+ l r) -1))
                                   (aref vector r)
                                   order)))
             (declare ((integer 0 #.most-positive-fixnum) l r))
             (loop (loop while (funcall order (aref vector l) pivot)
                         do (incf l))
                   (loop while (funcall order pivot (aref vector r))
                         do (decf r))
                   (when (>= l r)
                     (return))
                   (rotatef (aref vector l) (aref vector r))
                   (incf l 1)
                   (decf r 1))
             (recur left (- l 1))
             (recur (+ r 1) right)))))
    (recur start (- end 1))
    vector))

(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"
  (declare #.opt)
  (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 %sort (vec)
  (declare #.opt
           ((array uint31 (*)) vec))
  (quicksort! vec #'<))

(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))
      (%sort (the (array uint31 (*)) (aref ltable 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)
                                (minf res (- lx rx))
                                (return)))
                            (incf lpos))))
    (* 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))
      (%sort (the (array uint31 (*)) (aref rtable 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)
                                (minf res (- ux rx))
                                (return)))
                            (incf upos))))
    (* res 10)))

(defun main ()
  (declare #.opt)
  (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))))))

#-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 "2
11 1 U
11 47 D
"
    "230
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "4
20 30 U
30 20 R
20 10 D
10 20 L
"
    "SAFE
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "8
168 224 U
130 175 R
111 198 D
121 188 L
201 116 U
112 121 R
145 239 D
185 107 L
"
    "100
")))


Measured memory_bytes: 444608