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

public class fran_pairbfs_pisa {

	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 ans = bfs(g, A, B, canSee);
		System.out.println(ans == -1 ? "indistinguishable" : ans);
		reader.close();
	}
	
	static int bfs(int[][] g, int A, int B, boolean[] canSee) {
		Queue<State> Q = new LinkedList<>();
		Q.add(new State(A, B));
		int[][] dist = new int[g.length][g.length];
		for(int i = 0; i < g.length; i++) {
			Arrays.fill(dist[i], Integer.MAX_VALUE);
		}
		dist[A][B] = 0;
		while(!Q.isEmpty()) {
			State cur = Q.poll();
			if(canSee[cur.x] != canSee[cur.y]) {
				return dist[cur.x][cur.y];
			}
			// go left
			State left = new State(g[cur.x][0], g[cur.y][0]);
			if(dist[left.x][left.y] == Integer.MAX_VALUE) {
				dist[left.x][left.y] = 1 + dist[cur.x][cur.y];
				Q.add(left);
			}
			// go right
			State right = new State(g[cur.x][1], g[cur.y][1]);
			if(dist[right.x][right.y] == Integer.MAX_VALUE) {
				dist[right.x][right.y] = 1 + dist[cur.x][cur.y];
				Q.add(right);
			}
		}
		return -1;
	}
	
	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);
		}
		
	}
	
}
