public static void main(String[] args) throws Throwable {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		StringBuilder sb = new StringBuilder();
		int cas = 1;
		for (String line; (line=in.readLine())!=null; cas++ ) {
			int n = Integer.parseInt(line);
			if(n==0) break;
			int s = Integer.parseInt(in.readLine());
			mAdy = new double[n][n];
			for (int i = 0; i < mAdy.length; i++) {
				for (int j = 0; j < mAdy[i].length; j++) {
					mAdy[i][j]=Integer.MIN_VALUE;
				}
			}
			for (StringTokenizer st; (st = new StringTokenizer(in.readLine()))!=null; ) {
				int a = Integer.parseInt(st.nextToken( ));
				int b = Integer.parseInt(st.nextToken( ));
				if(a==0 && b == 0) break;
				mAdy[a-1][b-1] = 1;
			}
			mAdy = floydWarshall(mAdy);
			int max = Integer.MIN_VALUE;
			int nodoEnd = -1;
			for (int i = 0; i < mAdy.length; i++) {
				if( mAdy[s-1][i] > max ){
					max = (int) mAdy[s-1][i];
					nodoEnd = i;
				}
			}
			sb.append("Case "+cas+": The longest path from "+s+" has length "+max+", finishing at "+(nodoEnd+1)+".\n\n");
		}
		System.out.print( new String(sb) );
	}