/*
 * Translated from Dan Haffey's Python script by Tim Rolfe
 *
 * Dan found the algorithm somewhere on the web.  I have found some
 * references to it.
 *
 * Sosic and Ku report that the very FIRST publication was the following:
 *   W. Ahrens. Mathematische Unterhaltungen und Spiele (in German). B.G.
 *   Teubner (Publishing Company), Leipzig, 1918­1921.
 *
 * English publication in 1969:
 *   Hoffman, E.J., Loessi, J.C. and Moore, R.C. (1969): Constructions
 *   for the Solution of the m Queens Problem, Mathematics Magazine,
 *   p. 66-72.
 *
 * More recent publication:
 *   B. Bernhardsson. Explicit solutions to the n-queens problems for
 *   all n. ACM SIGART Bulletin, 2(2):7, Apr. 1991, ACM Press.
 */
import java.util.Scanner;

public class DanHaffey
{
   public static void main ( String[] args )
   {
      Scanner console = new Scanner(System.in);
      int j, k, n, half_n, nMax;
      boolean odd, special;

      if ( args.length == 0 )
         nMax = console.nextInt();
      else
         nMax = Integer.parseInt(args[0]);
      for ( n = 4; n <= nMax; n++ )
      {
         System.out.println (n);
         half_n = n/2;
         odd = ( n & 1 ) != 0;
         if ( odd )
            n--;
         // Special case is the 6k plus 2 board
         special = (n-2)%6 == 0;
         for ( j = 0; j < n; j++ )
         {
            if ( !special )
               if ( j < half_n )
                  k = 2 * j + 1;
               else
                  k = ( 2 * j ) % n;
            else
               if ( j < half_n )
                  k = ( half_n + 2 * j - 1 ) % n;
               else
                  k = ( half_n + 2 * j + 2 ) % n;
            System.out.printf ("%4d", k);
         }
         if ( odd )
            System.out.printf ("%4d", n++);
         System.out.println();
      }
   }
}
