#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; unordered_map, int, pair_hash> done; done.reserve(MI); vector q; q.reserve(MI); q.push_back({a, b, 0}); done[{a,b}] = 0; int I = 0; int m = 2e9; auto start = chrono::high_resolution_clock::now(); while(!q.empty()) { if((I&((1<<10)-1))==0){ auto now = chrono::high_resolution_clock::now(); if(chrono::duration>(now-start).count() > 1.3) break; } int l = q.back().l, r = q.back().r, d = q.back().v + 1; q.pop_back(); //cerr << l << ' ' << r << ' ' << d << endl; if(done[{l,r}] != d-1){ //cerr << "Not optimal; continue\n"; continue; } T tl{g[l].l, g[r].l}, tr{g[l].r, g[r].r}; if(rand() & (1<<5)) swap(tl, tr); for(T t : {tl, tr}) { int a = t.a, b = t.b; if(g[a].v != g[b].v) { //cerr << "New min: " << g[a].v << " " << g[b].v << endl; m = min(m, d); break; } if(d>=m) continue; //cerr << "ab: " << a << ' ' << b << endl; auto it = done.find({a,b}); bool push = true; if(it == done.end()) done[{a,b}] = d; else if(d < it->second) done[{a,b}] = d; else { push = false; //cerr << "nopush\n"; } if(push){ q.push_back({a, b, d}); //cerr << "push: " << a << ' ' << b << ' ' << d << endl; } } } if(m < 2e9) cout << m << endl; else cout << "INDISTINGUISHABLE" << endl; return 0; }