#include using namespace std; struct S { int l, r, v; }; struct T { int a, b; }; struct pair_hash { std::size_t operator()(const std::pair& p) const { return std::hash()(p.first) ^ (std::hash()(p.second) << 3); } }; int main() { int n, a, b; cin >> n >> a >> b; vector g(n); for(auto &x : g) cin >> x.l >> x.r >> x.v; if(g[a].v != g[b].v) { cout << 0 << endl; return 0; } constexpr int MI = 5e7; int I = 0; int m = n+10; auto start = chrono::high_resolution_clock::now(); while(true) { if((++I&((1<<10)-1))==0){ auto now = chrono::high_resolution_clock::now(); if(chrono::duration>(now-start).count() > 1.0) break; } int u=a, v=b, d=0; while(g[u].v == g[v].v && ++d < m){ if(rand() & (1<<5)) u = g[u].l, v = g[v].l; else u = g[u].r, v = g[v].r; } m = d; } if(m <= n) cout << m << endl; else cout << "INDISTINGUISHABLE" << endl; return 0; }