public static void main(String[] args) {
    int n = scn.nextInt();
    int tc = 0;
    while (n != 0) {
      // Create the nodes and put them in the map
      Point[] points = new Point[n + 1];
      for (int i = 1; i <= n; i++) {
        points[i] = new Point(i);
      }

      // The starting point
      int s = scn.nextInt();

      // The edges
      int p = scn.nextInt();
      int q = scn.nextInt();
      while (p != 0) {
        points[p].adjacent.add(points[q]);

        p = scn.nextInt();
        q = scn.nextInt();
      }

      // The BFS:
      List<Point> queue = new LinkedList<Point>();
      queue.add(points[s]);

      while (!queue.isEmpty()) {
        Point top = queue.remove(0);

        for (Point next : top.adjacent) {
          if (next.distance < top.distance + 1) {
            next.distance = top.distance + 1;
            queue.add(next);
          }
        }
      }

      // Gets the maximum level
      Point finalPoint = points[s];
      for (int i = 1; i <= n; i++) {
        if (points[i].distance > finalPoint.distance) {
          finalPoint = points[i];
        }
      }
      System.out
          .printf(
              "Case %d: The longest path from %d has length %d, finishing at %d.\n",
              ++tc, s, finalPoint.distance, finalPoint.id);
      n = scn.nextInt();
    }
  }