Scanf.scanf "%d" (fun n ->
    let rec loop i u d r l =
        if i = n then u, d, r, l else
            let x, y, dir = Scanf.scanf " %d %d %c" (fun x y u -> x, y, u) in
            match dir with
            | 'U' -> loop (i + 1) ((x, y) :: u) d r l
            | 'D' -> loop (i + 1) u ((x, y) :: d) r l
            | 'R' -> loop (i + 1) u d ((x, y) :: r) l
            | 'L' -> loop (i + 1) u d r ((x, y) :: l)
            | _ -> failwith "??"
    in
    let proc sc a1 a2 f1 f2 =
        let arena = Array.make 400001 [] in
        List.iter (fun xy -> let p, v, dir = f1 xy in arena.(p) <- (v, dir) :: arena.(p)) a1;
        List.iter (fun xy -> let p, v, dir = f2 xy in arena.(p) <- (v, dir) :: arena.(p)) a2;
        let count l =
            let rec loop m = function
                | (h1, 1) :: ((h2, -1) as h) :: tl -> loop (min m ((h2 - h1) * sc)) (h :: tl)
                | _ :: h :: tl -> loop m (h :: tl)
                | _ -> m
            in
            loop max_int (List.sort compare l)
        in
        Array.fold_left (fun acc l -> min acc (count l)) max_int arena
    in
    let u, d, r, l = loop 0 [] [] [] [] in
    let m1 = proc  5 u d (fun (x, y) -> x,              y, 1) (fun (x, y) -> x,              y, -1) in
    let m2 = proc  5 r l (fun (x, y) -> y,              x, 1) (fun (x, y) -> y,              x, -1) in
    let m3 = proc 10 u r (fun (x, y) -> x + y,          y, 1) (fun (x, y) -> x + y,          y, -1) in
    let m4 = proc 10 l d (fun (x, y) -> x + y,          y, 1) (fun (x, y) -> x + y,          y, -1) in
    let m5 = proc 10 u l (fun (x, y) -> x - y + 200000, y, 1) (fun (x, y) -> x - y + 200000, y, -1) in
    let m6 = proc 10 r d (fun (x, y) -> x - y + 200000, y, 1) (fun (x, y) -> x - y + 200000, y, -1) in
    let ( *@) a b = min a b in
    let c = m1 *@ m2 *@ m3 *@ m4 *@ m5 *@ m6 in
    if c = max_int then print_endline "SAFE" else Printf.printf "%d\n" c
)