package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)

func Scan() string {
	sc.Scan()
	return sc.Text()
}
func iScan() int {
	n, _ := strconv.Atoi(Scan())
	return n
}
func fScan() float64 {
	n, _ := strconv.ParseFloat(Scan(), 64)
	return n
}
func stringToInt(s string) int {
	n, _ := strconv.Atoi(s)
	return n
}
func SScan(n int) []string {
	a := make([]string, n)
	for i := 0; i < n; i++ {
		a[i] = Scan()
	}
	return a
}
func iSScan(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = iScan()
	}
	return a
}
func abs(x int) int {
	if x < 0 {
		x = -x
	}
	return x
}
func larger(a, b int) int {
	if a < b {
		return b
	} else {
		return a
	}
}
func smaller(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}
func max(a []int) int {
	x := a[0]
	for i := 0; i < len(a); i++ {
		if x < a[i] {
			x = a[i]
		}
	}
	return x
}
func min(a []int) int {
	x := a[0]
	for i := 0; i < len(a); i++ {
		if x > a[i] {
			x = a[i]
		}
	}
	return x
}
func sum(a []int) int {
	x := 0
	for _, v := range a {
		x += v
	}
	return x
}

var mod int = 1000000007

func main() {
	buf := make([]byte, 0)
	sc.Buffer(buf, mod)
	sc.Split(bufio.ScanWords)
	n := iScan()
	pd, pu, pr, pl := make(map[int][]int), make(map[int][]int), make(map[int][]int), make(map[int][]int)
	md, mu, mr, ml := make(map[int][]int), make(map[int][]int), make(map[int][]int), make(map[int][]int)
	yd, yu, yr, yl := make(map[int][]int), make(map[int][]int), make(map[int][]int), make(map[int][]int)
	for i := 0; i < n; i++ {
		x, y := iScan(), iScan()
		u := Scan()
		if u == "D" {
			pd[y-x] = append(pd[y-x], x)
			md[y+x] = append(md[y+x], x)
			yd[x] = append(yd[x], y)
		} else if u == "U" {
			pu[y-x] = append(pu[y-x], x)
			mu[y+x] = append(mu[y+x], x)
			yu[x] = append(yu[x], y)
		} else if u == "R" {
			pr[y-x] = append(pr[y-x], x)
			mr[y+x] = append(mr[y+x], x)
			yr[y] = append(yr[y], x)
		} else if u == "L" {
			pl[y-x] = append(pl[y-x], x)
			ml[y+x] = append(ml[y+x], x)
			yl[y] = append(yl[y], x)
		}
	}
	for k, _ := range pd {
		sort.Ints(pd[k])
	}
	for k, _ := range pu {
		sort.Ints(pu[k])
	}
	for k, _ := range pr {
		sort.Ints(pr[k])
	}
	for k, _ := range pl {
		sort.Ints(pl[k])
	}
	for k, _ := range md {
		sort.Ints(md[k])
	}
	for k, _ := range mu {
		sort.Ints(mu[k])
	}
	for k, _ := range mr {
		sort.Ints(mr[k])
	}
	for k, _ := range ml {
		sort.Ints(ml[k])
	}
	for k, _ := range yd {
		sort.Ints(yd[k])
	}
	for k, _ := range yu {
		sort.Ints(yu[k])
	}
	for k, _ := range yr {
		sort.Ints(yr[k])
	}
	for k, _ := range yl {
		sort.Ints(yl[k])
	}
	ans := mod * mod
	for k, list := range pd {
		z := pr[k]
		for _, v := range list {
			if len(z) != 0 {
				idx := sort.SearchInts(z, v)
				if idx == 0 {
					continue
				} else {
					ans = smaller(ans, abs(v-z[idx-1])*10)
				}
			}
		}
	}
	for k, list := range ml {
		z := md[k]
		for _, v := range list {
			if len(z) != 0 {
				idx := sort.SearchInts(z, v)
				if idx == 0 {
					continue
				} else {
					ans = smaller(ans, abs(v-z[idx-1])*10)
				}
			}
		}
	}
	for k, list := range pl {
		z := pu[k]
		for _, v := range list {
			if len(z) != 0 {
				idx := sort.SearchInts(z, v)
				if idx == 0 {
					continue
				} else {
					ans = smaller(ans, abs(v-z[idx-1])*10)
				}
			}
		}
	}
	for k, list := range mu {
		z := mr[k]
		for _, v := range list {
			if len(z) != 0 {
				idx := sort.SearchInts(z, v)
				if idx == 0 {
					continue
				} else {
					ans = smaller(ans, abs(v-z[idx-1])*10)
				}
			}
		}
	}
	for k, list := range yd {
		z := yu[k]
		for _, v := range list {
			if len(z) != 0 {
				idx := sort.SearchInts(z, v)
				if idx == 0 {
					continue
				} else {
					ans = smaller(ans, abs(v-z[idx-1])*5)
				}
			}
		}
	}
	for k, list := range yl {
		z := yr[k]
		for _, v := range list {
			if len(z) != 0 {
				idx := sort.SearchInts(z, v)
				if idx == 0 {
					continue
				} else {
					ans = smaller(ans, abs(v-z[idx-1])*5)
				}
			}
		}
	}
	if ans == mod*mod {
		fmt.Println("SAFE")
	} else {
		fmt.Println(ans)
	}
}


Measured memory_bytes: 93888