Problem:
Deadlock Detection

In concurrent processing environments, a deadlock is an undesirable
situation where two or more threads are mutually waiting for others to
finish using some resources and cannot proceed further.

Your task is to detect whether there is any possibility of deadlocks
when multiple threads try to execute a given instruction sequence concurrently.

The instruction sequence consists of characters 'u' or digits from
'0' to '9', and each of them represents one instruction.
10 threads are trying to execute the same single instruction sequence.
Each thread starts its execution from the beginning of the sequence and
continues in the given order, until all the instructions are
executed.

There are 10 shared resources called locks from L0 to L9.
A digit k is the instruction for acquiring the lock Lk.
After one of the threads acquires a lock Lk,
it is kept by the thread until it is released by the instruction 'u'.
While a lock is kept, none of the threads, including one already acquired it,
cannot newly acquire the same lock Lk.

Precisely speaking, the following steps are repeated until all threads
finish.

One thread that has not finished yet is chosen arbitrarily.

The chosen thread tries to execute the next instruction that is not executed yet.

If the next instruction is a digit k and
the lock Lk is not kept by any thread,
the thread executes the instruction k and acquires Lk.

If the next instruction is a digit k and
the lock Lk is already kept by some thread,
the instruction k is not executed.

If the next instruction is 'u',
the instruction is executed and all the locks currently kept by the thread
are released.

After executing several steps, sometimes, it falls into the situation
that the next instructions
of all unfinished threads are for acquiring already kept locks.
Once such a situation happens, no instruction will ever be executed no
matter which thread is chosen. This situation is called a deadlock.

There are instruction sequences for which threads can never reach a
deadlock regardless of the execution order.
Such instruction sequences are called safe.
Otherwise, in other words, if there exists one or more execution orders
that lead to a deadlock, the execution sequence is called unsafe.

Your task is to write a program that tells whether the given instruction sequence is safe or unsafe.

Input

The input consists of at most 50 datasets, each in the following format.

n

s

n is the length of the instruction sequence and s is a string representing the sequence.
n is a positive integer not exceeding 10,000.
Each character of s is either a digit ('0' to '9') or 'u',
and s always ends with 'u'.

The end of the input is indicated by a line with a zero.

Output

For each dataset, if the given instruction sequence is safe, then print "SAFE" in a line.
If it is unsafe, then print "UNSAFE" in a line.

Sample Input

11
01u12u0123u
6
01u10u
8
201u210u
9
01u12u20u
3
77u
12
9u8u845u954u
0

Output for the Sample Input

SAFE
UNSAFE
SAFE
UNSAFE
UNSAFE
UNSAFE

The second input "01u10u" may possibly cause a deadlock. After one
thread has executed the initial four instructions "01u1", the
thread keeps only one lock L1. If another thread executes the first
instruction '0' at this time, this thread acquires L0. Then, the
first thread tries to acquire L0, already kept by the second
thread, while the second thread tries to acquire L1, kept by the
first thread; This leads to a deadlock.

→
→

Figure 1: Why the Sample Input 2 "01u10u" is unsafe.

Contrarily, the third input "201u210u" is safe.
If one thread had executed up to "201u21" and another to "20", then
one may think it would lead to a deadlock, but this
can never happen because no two threads can simultaneously keep L2.

Solution in C++14 (judge verdict: Wrong Answer)

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define int ll
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define PB push_back

const ll LLINF = (1LL<<60);
const int INF = (1LL<<30);
const int MOD = 1000000007;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; }
template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }
template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a){
  out<<'('<<a.first<<','<<a.second<<')';
  return out;
}
template<class T>
ostream &operator <<(ostream& out,const vector<T>& a){
  out<<'[';
  REP(i, a.size()) {out<<a[i];if(i!=a.size()-1)out<<',';}
  out<<']';
  return out;
}

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

// VI tsort(VVI g) {
//   int n = g.size();
//   vector<bool> used(n, false);
//   vector<int> ans;
//
//   function<void(int)> dfs = [&](int u) {
//     if(used[u]) return;
//     used[u] = true;
//     for(auto& i: g[u]) dfs(i);
//     ans.push_back(u);
//   };
//
//   for(int i=0; i<n; ++i) dfs(i);
//   reverse(ans.begin(), ans.end());
//   return ans;
// }

VI tsort(VVI g) {
  const int n = g.size();
  VI h(n, 0);
  REP(i, n) for(int j: g[i]) h[j]++;

  stack<int> st;
  REP(i, n) if(h[i] == 0) st.push(i);

  VI ans;
  while(st.size()) {
    int i = st.top(); st.pop();
    ans.push_back(i);
    for(auto& j: g[i]) {
      h[j]--;
      if(h[j] == 0) st.push(j);
    }
  }

  return ans;
}

class UnionFind {
public:
  const static int MAX_N = 10010;
  int par[MAX_N];
  int s[MAX_N];
  UnionFind() { init(); }
  UnionFind(int n) { init(n); }
  void init() { for(int i=0; i<MAX_N; ++i) par[i] = i, s[i] = 1; }
  void init(int n) { for(int i=0; i<n; ++i) par[i] = i, s[i] = 1; }
  int find(int x) {
    if(par[x] == x) return x;
    return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if(x == y) return;
    if(s[x] < s[y]) par[x] = y, s[y] = s[x] + s[y];
    else par[y] = x, s[x] = s[x] + s[y];
  }
  bool same(int x, int y) { return find(x) == find(y);}
};
UnionFind uf;

signed main(void)
{
  cin.tie(0);
  ios::sync_with_stdio(false);

  while(true) {
    bool safe;
    int n;
    cin >> n;
    if(!n) break;
    string s;
    cin >> s;

    safe = true;
    vector<PII> v(n);
    int now = 0;
    REP(i, n) {
      int nxt = -1;
      if(i < n && isdigit(s[i])) nxt = s[i] - '0';
      v[i] = {now, nxt};

      if(s[i] == 'u') {
        now = 0;
      } else {
        if(now & 1 << (s[i] - '0')) {
          safe = false;
          break;
        }
        now |= 1 << (s[i] - '0');
      }
    }
    if(!safe) {
      cout << "UNSAFE" << endl;
      continue;
    }

    VVI g(n, VI(0));
    REP(i, n) FOR(j, i+1, n) {
      if(v[i].second == -1 || v[j].second == -1) continue;
      if(v[i].first & v[j].first) continue;
      if((v[i].first & 1 << v[j].second)) {
        // j->iに辺を貼る
        g[j].PB(i);
      }
      if(v[j].first & 1 << v[i].second) {
        // i->jに辺を貼る
        g[i].PB(j);
      }
    }

    VI ans = tsort(g);
    if(ans.size() != n) safe = false;

    vector<bool> loop(n, true);
    REP(i, ans.size()) loop[ans[i]] = false;

    // 長さ10より大きいループがあってもデッドロックは起きない
    // トポソしてansにないやつ→ループに使われてる
    // UFでつないで要素数が11以上の連結成分しかなければsafe
    uf.init();
    REP(i, n) {
      if(!loop[i]) continue;
      for(int j: g[i]) {
        uf.unite(i, j);
      }
    }
    REP(i, n) {
      if(!loop[i]) continue;
      if(uf.s[uf.find(i)] <= 10) {
        safe = false;
        break;
      }
    }

    if(safe) cout << "SAFE" << endl;
    else cout << "UNSAFE" << endl;
  }

  return 0;
}