import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class fran_pairbfs_pisa_2bfs {

	public static void main(String[] args) {
		Scanner reader = new Scanner(System.in);
		int n = reader.nextInt();
		int A = reader.nextInt();
		int B = reader.nextInt();
		int[][] g = new int[n][2];
		boolean[] canSee = new boolean[n];
		for(int i = 0; i < g.length; i++) {
			g[i][0] = reader.nextInt();
			g[i][1] = reader.nextInt();
			canSee[i] = reader.nextInt() == 1;
		}
		int bfsA = bfs(g, A, A, B, canSee);
		int bfsB = bfs(g, B, A, B, canSee);
		int ans = Math.min(bfsA, bfsB);
		System.out.println(ans == Integer.MAX_VALUE ? "indistinguishable" : ans);
		reader.close();
	}
	
	static int bfs(int[][] g, int s, int A, int B, boolean[] canSee) {
		int[] d = new int[g.length];
		Arrays.fill(d, Integer.MAX_VALUE);
		d[s] = 0;
 		Queue<Integer> Q = new LinkedList<>();
		Q.add(s);
		State[] state = new State[g.length];
		state[s] = new State(A, B);
		while(!Q.isEmpty()) {
			int cur = Q.poll();
			if(canSee[state[cur].x] != canSee[state[cur].y]) return d[cur];
			int left = g[cur][0];
			if(state[left] == null) {
				d[left] = 1 + d[cur];
				state[left] = new State(g[state[cur].x][0], g[state[cur].y][0]);
				Q.add(left);
			}
			int right = g[cur][1];
			if(state[right] == null) {
				d[right] = 1 + d[cur];
				state[right] = new State(g[state[cur].x][1], g[state[cur].y][1]);
			}
		}
		return Integer.MAX_VALUE;
	}	

	
	static class State {
		
		int x, y;
		
		public State(int x, int y) {
			this.x = x;
			this.y = y;
		}
		
		public String toString() {
			return String.format("(%d, %d)", x, y);
		}
		
	}
	
}
