#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
const int MAX_N = 200000; //! 200000
int main() {
    int n;
    scanf("%i", &n);
    vector<int> Down_x[MAX_N + 1], Up_x[MAX_N + 1],
                Right_y[MAX_N + 1], Left_y[MAX_N + 1];
    vector<int> Right_d1[2 * MAX_N + 1], Right_d0[2 * MAX_N + 1],
                Left_d1[2 * MAX_N + 1], Left_d0[2 * MAX_N + 1],
                Up_d1[2 * MAX_N + 1], Up_d0[2 * MAX_N + 1],
                Down_d1[2 * MAX_N + 1], Down_d0[2 * MAX_N + 1];
    for (int i = 0; i < n; i++) {
        int x, y;
        char u;
        scanf("%i%i%*c%c", &x, &y, &u);
        switch (u) {
        case 'U':
            Up_x[x].push_back(y);
            Up_d1[x - y + MAX_N].push_back(x);
            Up_d0[x + y].push_back(x);
            break;
        case 'D':
            Down_x[x].push_back(y);
            Down_d1[x - y + MAX_N].push_back(x);
            Down_d0[x + y].push_back(x);
            break;
        case 'R':
            Right_y[y].push_back(x);
            Right_d1[x - y + MAX_N].push_back(x);
            Right_d0[x + y].push_back(x);
            break;
        case 'L':
            Left_y[y].push_back(x);
            Left_d1[x - y + MAX_N].push_back(x);
            Left_d0[x + y].push_back(x);
        }
    }
    for (int i = 0; i <= MAX_N; i++) {
        sort(Up_x[i].begin(), Up_x[i].end());
        sort(Down_x[i].begin(), Down_x[i].end());
        sort(Left_y[i].begin(), Left_y[i].end());
        sort(Right_y[i].begin(), Right_y[i].end());
    }
    for (int i = 0; i <= 2 * MAX_N; i++) {
        sort(Up_d0[i].begin(), Up_d0[i].end());
        sort(Up_d1[i].begin(), Up_d1[i].end());

        sort(Down_d0[i].begin(), Down_d0[i].end());
        sort(Down_d1[i].begin(), Down_d1[i].end());

        sort(Right_d0[i].begin(), Right_d0[i].end());
        sort(Right_d1[i].begin(), Right_d1[i].end());

        sort(Left_d0[i].begin(), Left_d0[i].end());
        sort(Left_d1[i].begin(), Left_d1[i].end());
    }

    int ans = 7777777;
    /// Up & Down
    for (int x = 0; x <= MAX_N; x++) {
        int i_down = 0;
        for (int i_up = 0; i_up < Up_x[x].size(); i_up++) {
            while (i_down < Down_x[x].size()
                   && Down_x[x][i_down] < Up_x[x][i_up]) {
                i_down++;
            }
            if (i_down < Down_x[x].size()
                && ans > (Down_x[x][i_down] - Up_x[x][i_up]) * 5) {
                ans = (Down_x[x][i_down] - Up_x[x][i_up]) * 5;
            }
        }
    }
    /// Left & Right
    for (int y = 0; y <= MAX_N; y++) {
        int i_l = 0;
        for (int i_r = 0; i_r < Right_y[y].size(); i_r++) {
            while (i_l < Left_y[y].size()
                   && Left_y[y][i_l] < Right_y[y][i_r]) {
                i_l++;
            }
            if (i_l < Left_y[y].size()
                && ans > (Left_y[y][i_l] - Right_y[y][i_r]) * 5) {
                ans = (Left_y[y][i_l] - Right_y[y][i_r]) * 5;
            }
        }
    }
    /// Up & Left
    for (int d1 = 0; d1 <= 2 * MAX_N; d1++) {
        int i_l = 0;
        for (int i_up = 0; i_up < Up_d1[d1].size(); i_up++) {
            while (i_l < Left_d1[d1].size()
                   && Left_d1[d1][i_l] < Up_d1[d1][i_up]) {
                i_l++;
            }
            if (i_l < Left_d1[d1].size()
                && ans > (Left_d1[d1][i_l] - Up_d1[d1][i_up]) * 10) {
                ans = (Left_d1[d1][i_l] - Up_d1[d1][i_up]) * 10;
            }
        }
    }
    /// Down & Right
    for (int d1 = 0; d1 <= 2 * MAX_N; d1++) {
        int i_d = 0;
        for (int i_r = 0; i_r < Right_d1[d1].size(); i_r++) {
            while (i_d < Down_d1[d1].size()
                   && Down_d1[d1][i_d] < Right_d1[d1][i_r]) {
                i_d++;
            }
            if (i_d < Down_d1[d1].size()
                && ans > (Down_d1[d1][i_d] - Right_d1[d1][i_r]) * 10) {
                ans = (Down_d1[d1][i_d] - Right_d1[d1][i_r]) * 10;
            }
        }
    }
    /// Down & Left
    for (int d0 = 0; d0 <= 2 * MAX_N; d0++) {
        int i_l = 0;
        for (int i_d = 0; i_d < Down_d0[d0].size(); i_d++) {
            while (i_l < Left_d0[d0].size()
                   && Left_d0[d0][i_l] < Down_d0[d0][i_d]) {
                i_l++;
            }
            if (i_l < Left_d0[d0].size()
                && ans > (Left_d0[d0][i_l] - Down_d0[d0][i_d]) * 10) {
                ans = (Left_d0[d0][i_l] - Down_d0[d0][i_d]) * 10;
            }
        }
    }
    /// Up & Right
    for (int d0 = 0; d0 <= 2 * MAX_N; d0++) {
        int i_up = 0;
        for (int i_r = 0; i_r < Right_d0[d0].size(); i_r++) {
            while (i_up < Up_d0[d0].size()
                   && Up_d0[d0][i_up] < Right_d0[d0][i_r]) {
                i_up++;
            }
            if (i_up < Up_d0[d0].size()
                && ans > (Up_d0[d0][i_up] - Right_d0[d0][i_r]) * 10) {
                ans = (Up_d0[d0][i_up] - Right_d0[d0][i_r]) * 10;
            }
        }
    }
    if (ans != 7777777) printf("%i", ans);
    else printf("SAFE");
}


Measured memory_bytes: 115704