// Arup Guha
// Originally written for the 6/29/20 UCF practice contest
// Edited on 10/6/04, to clean up the code and add comments.
// This program solves the problem girth, which essentially finds the longest path between
// any two nodes in a graph.

import java.io.*;
import java.util.*;

public class girth {

  public static void main(String[] args) throws IOException {

    BufferedReader fin = new BufferedReader(new FileReader("girth.in"));

    int grnum = 1;
    
    // Read in until there's no more input.
    while (fin.ready()) {

      int num = Integer.parseInt(fin.readLine());
      if (num == 0) break;
      
      // Initialize the adjacency matrix.
      int[][] m = new int[num][num];
      for (int a = 0; a<num; a++)
        for (int b=0; b<num; b++)
          m[a][b] = 10000;
      for (int a=0; a<num; a++)
        m[a][a] = 0;

      // Read in the adjacency matrix.
      for (int i=0; i<num; i++) {
        StringTokenizer temp = new StringTokenizer(fin.readLine());
        while (temp.hasMoreTokens()) {
          int val = Integer.parseInt(temp.nextToken());
          m[i][val-1] = 1;
          m[val-1][i] = 1;
        }
      }
  
      // Run floyd-warshall's here.
      for (int k=0; k<num; k++) {
        
        for (int i=0; i<num; i++) {
          for (int j=0; j<num;j++) {
            m[i][j] = min(m[i][j], m[i][k]+m[k][j]);
          }
        }
      }

      // Get the answer here.
      int ans = max(m);    

      // This is a hack. If there is one node in the graph, the girth is 
      // automatically 0. I am not sure why this case wasn't working
      // correctly before.
      if (num == 1) 
        ans = 0;  

      // Print out the result for this graph.
      System.out.print("Girth for graph #"+grnum);

      if (ans > 9999) 
        System.out.println(" = Infinite");
      else 
        System.out.println(" = "+ans);
      System.out.println();

      grnum++;

    }    
    fin.close();
  }

  // Returns the larger of a and b.
  public static int min(int a, int b) {
    if (a < b)
      return a;
    else
      return b;
  }

  // Returns the largest value stored in the array a.
  public static int max(int[][] a) {
   
    int t = a[0][0];
    int sz = a.length;
 
    // Loop through all values and update the max as necessary.
    for (int i=0; i<sz; i++)
      for (int j=0; j<sz; j++)
        if (a[i][j] > t) 
          t = a[i][j];

    return t;

  }

}
