#include<bits/stdc++.h>
using namespace std;
#define RI register int
int read() {
	int q=0;char ch=' ';
	while(ch<'0'||ch>'9') ch=getchar();
	while(ch>='0'&&ch<='9') q=q*10+ch-'0',ch=getchar();
	return q;
}
const int N=100005;
int n,a[4][N],b[N],ans[2];
void NO() {puts("No");exit(0);}
int main()
{
	n=read();
	for(RI i=1;i<=3;++i)
		for(RI j=1;j<=n;++j) {
			a[i][j]=read();
			int x=(a[i][j]-1)%3+1,y=(a[i][j]+2)/3;
			if((i!=2&&x==2)||(i==2&&x!=2)) NO();
			if((j&1)^(y&1)) NO();
		}
	for(RI j=1;j<=n;++j) {
		int x=(a[1][j]-1)%3+1,y=(a[1][j]+2)/3;
		if((a[2][j]+2)/3!=y||(a[3][j]+2)/3!=y) NO();
		if(x==1&&(a[3][j]-1)%3+1!=3) NO();
		if(x==3&&(a[3][j]-1)%3+1!=1) NO();
		if(x==3) ans[j&1]^=1;
		b[j]=y;
	}
	for(RI i=1;i<=n;++i)
		while(b[i]!=i) ans[(i&1)^1]^=1,swap(b[i],b[b[i]]);
	if(ans[0]||ans[1]) NO();
	puts("Yes");
	return 0;
}
#include<bits/stdc++.h>
using namespace std;

int n, x[1<<18], y[1<<18];
char d[1<<18];

void Rotate(){
  for(int i = 1; i <= n; i++){
    int cx = y[i],cy = 200000-x[i],cz;
    if(d[i] == 'U') cz = 'R';
    if(d[i] == 'R') cz = 'D';
    if(d[i] == 'D') cz = 'L';
    if(d[i] == 'L') cz = 'U';
    x[i] = cx; y[i] = cy; d[i] = cz;
  }
}

vector<pair<int,char>> v1[1<<19];
vector<pair<int,char>> v2[1<<19];

int solve_01(){
  int ans = (1 << 24);
  for(int i = 0; i <= 500000; i++) v1[i].clear();
  for(int i = 1; i <= n; i++){
    if(!(d[i] == 'R' || d[i] == 'U')) continue;
    v1[x[i]+y[i]].push_back(make_pair(x[i],d[i]));
  }
  for(int i = 0; i <= 500000; i++){
    sort(v1[i].begin(),v1[i].end());
    for(int j = 0; j < (int)v1[i].size() - 1; j++){
     if(!(v1[i][j].second == 'R' && v1[i][j+1].second == 'U')) continue;
     ans = min(ans,v1[i][j+1].first - v1[i][j].first);
    }
  }
   return ans * 10;
}

int solve_02(){
  int ans = (1 << 24);
  for(int i = 0; i <= 500000; i++) v2[i].clear();
  for(int i = 1; i <= n; i++){
    if(!(d[i] == 'L' || d[i] == 'R')) continue;
    v2[y[i]].push_back(make_pair(x[i],d[i]));
  }
  for(int i = 0; i <= 500000; i++){
    sort(v2[i].begin(),v2[i].end());
    for(int j = 0; j < (int) v2[i].size()-1; j++){
      if(!(v2[i][j].second == 'R' && v2[i][j+1].second == 'L')) continue;
      ans = min(ans,v2[i][j+1].first - v2[i][j].first);
    }
  }
  return ans*5;
 }

 int main(){
  cin >> n;
  for(int i = 1; i <= n; i++) cin >> x[i] >> y[i] >> d[i];
  int fans = (1 << 24);
  for(int i = 1; i <= 4; i++){
    int val1 = solve_01(); fans = min(fans,val1);
    int val2 = solve_02(); fans = min(fans,val2);
    Rotate();
  }
  if(fans == (1 << 24)) cout << "SAFE" << endl;
  else cout << fans << endl;
  return 0;
}