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

public class fran_pairbfs_pisa_uf {

	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 class DisjointSet {

		private int[] rank, p, size;
		private int nSets;

		public DisjointSet(int n) {
			nSets = n;
			size = new int[n];
			rank = new int[n];
			p = new int[n];
			for (int i = 0; i < p.length; i++) {
				p[i] = i;
				size[i] = 1;
			}
		}

		public boolean union(int x, int y) {
			if (find(x) != find(y)) {
				link(find(x), find(y));
				nSets--;
				return true;
			}
			return false;
		}

		private void link(int x, int y) {
			if (rank[x] > rank[y]) {
				p[y] = x;
				size[x] += size[y];
			} else {
				p[x] = y;
				size[y] += size[x];
				if (rank[x] == rank[y]) {
					rank[y]++;
				}
			}
		}

		public int find(int x) {
			if (x != p[x]) {
				p[x] = find(p[x]);
			}
			return p[x];
		}

		public int nSets() {
			return nSets;
		}

	}

	static int bfs(int[][] g, int A, int B, boolean[] canSee) {
		DisjointSet DS = new DisjointSet(g.length);
		Queue<State> Q = new LinkedList<>();
		State s = new State(A, B);
		Q.add(new State(A, B));
		HashMap<State, Integer> dist = new HashMap<>();
		dist.put(s, 0);
		while(!Q.isEmpty()) {
			State cur = Q.poll();
			if(canSee[cur.x] != canSee[cur.y]) {
				return dist.get(cur);
			}
			int dcur = dist.get(cur);
			// go left
			State left = new State(g[cur.x][0], g[cur.y][0]);
			if(DS.union(left.x, left.y)) {
				dist.put(left, 1 + dcur);
				DS.union(left.x, left.y);
				Q.add(left);

			}
			// go right
			State right = new State(g[cur.x][1], g[cur.y][1]);
			if(DS.union(right.x, right.y)) {
				dist.put(right, 1 + dcur);
				DS.union(right.x, right.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 boolean equals(Object other) {
			State o = (State)other;
			return x == o.x && y == o.y;
		}
		
		public int hashCode() {
			return x + 31 * y;
		}

		public String toString() {
			return String.format("(%d, %d)", x, y);
		}

	}

}
