#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); 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; } queue q; q.emplace(a, b); ii marker(a, b); int ret = 0; ii lastAdded(a, b); while (!q.empty()) { ii cur = q.front(); q.pop(); if (uf.find(cur.x) == uf.find(cur.y)) { // assert(T[cur.x] == T[cur.y]); if (cur == marker) { marker = lastAdded; ret++; } continue; } if (T[cur.x] != T[cur.y]) { cout << ret << endl; return 0; } uf.merge(cur.x, cur.y); q.emplace(L[cur.x], L[cur.y]); q.push(lastAdded = ii(R[cur.x], R[cur.y])); // increase height. if (cur == marker) { marker = lastAdded; ret++; } } cout << "indistinguishable" << endl; return 0; }