#include <bits/stdc++.h>
#define rep(x, l, r) for(int x = l; x <= r; x++)
#define repd(x, r, l) for(int x = r; x >= l; x--)
#define clr(x, y) memset(x, y, sizeof(x))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define SZ(x) ((int)x.size())
#define int long long
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 0x3f3f3f3f;
const int MAXN = 1105;
pii dp[MAXN][MAXN];
bool vis[MAXN][MAXN];
struct node{
int a, b, c, d;
} g[MAXN];
int f[MAXN];
const int M = 1e7;
int cnt = 0;
pii dfs(int u, int a){
cnt++;
if(cnt > M) return mp(0, -1);
if(!f[u] && !a) return mp(1, 0);
if(f[u] && g[u].a == a) return mp(1, g[u].b);
if(dp[u][a].fi != -1) return dp[u][a];
if(vis[u][a]) return dp[u][a] = mp(0, -1);
vis[u][a] = 1;
pii res = dfs(g[u].d, g[u].c);
if(res.se == -1) return dp[u][a] = mp(0, -1);
if(!res.se) return dp[u][a] = mp(res.fi + 1, 0);
pii res2 = dfs(res.se, a);
vis[u][a] = 0;
return dp[u][a] = mp(res.fi + res2.fi + 1, res2.se);
}
int main(){
int n;
scanf("%lld", &n);
getchar();
rep(i, 1, n){
char ch = getchar();
if(ch == 'P'){
f[i] = 1;
scanf("OP %lld GOTO %lld; PUSH %lld GOTO %lld", &g[i].a, &g[i].b, &g[i].c, &g[i].d);
}
else{
f[i] = 0;
scanf("ALT; PUSH %lld GOTO %lld", &g[i].c, &g[i].d);
}
getchar();
}
// rep(i, 1, n){
// printf("%lld : %lld %lld %lld %lld\n", f[i], g[i].a, g[i].b, g[i].c, g[i].d);
// }
clr(dp, -1);
// rep(i, 1, n){
// rep(j, 1, n) printf("%lld %lld %lld %lld\n", i, j, dp[i][j].fi, dp[i][j].se);
// }
pii ans = dfs(1, 0);
if(ans.se == -1) puts("-1");
else printf("%lld\n", ans.fi);
return 0;
}