#include using namespace std; #define x first #define y second #define pb push_back #define eb emplace_back #define rep(i,a,b) for(auto i = (a); i != (b); ++i) #define REP(i,n) rep(i,0,n) #define all(v) (v).begin(), (v).end() typedef long long ll; typedef long double ld; typedef pair ii; typedef vector vi; typedef vector vvi; typedef vector vii; struct UF { vi par, rnk; UF() : par(), rnk() {} UF(int n) : par(n, -1), rnk(n, 0) {} int find(int u) { return par[u] < 0 ? u : par[u] = find(par[u]); } bool merge(int u, int v) { if ((u = find(u)) == (v = find(v))) return false; if (rnk[u] > rnk[v]) swap(u, v); if (rnk[u] == rnk[v]) rnk[v]++; par[u] = v; return true; } }; const int maxn = 1e6; const int INF = 1e9; int N, L[maxn], R[maxn]; bool T[maxn]; UF uf(maxn); template struct hash< std::pair > { private: const hash ah; const hash bh; public: hash() : ah(), bh() {} size_t operator()(const std::pair &p) const { return (ah(p.first) << 6) ^ bh(p.second); } }; unordered_set active; int dfs(int a, int b, int depth = 0) { if (a > b) swap(a, b); if (T[a] != T[b]) return 0; // hacky stuff: if (depth >= N) return INF; int fa = uf.find(a), fb = uf.find(b); // if (uf.find(a) == uf.find(b)) if (fa == fb) return INF; // indistinguishable if (active.find(ii(a, b)) != active.end()) return INF; // do not use cycles if (active.find(ii(fa, fb)) != active.end()) return INF; // do not use cycles active.emplace(a, b); active.emplace(fa, fb); int ldfs = dfs(L[a], L[b], depth + 1); int rdfs = dfs(R[a], R[b], depth + 1); if (ldfs == INF && rdfs == INF) { uf.merge(fa, fb); return INF; } else { return min(ldfs, rdfs) + 1; } active.erase(ii(a, b)); active.erase(ii(fa, fb)); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int a, b; cin >> N >> a >> b; REP(i, N) { int t; cin >> L[i] >> R[i] >> t; T[i] = t != 0; } int ret = dfs(a, b); if (ret == INF) cout << "indistinguishable" << endl; else cout << ret << endl; return 0; }