{$apptype console}

// (C) Timofey S. Chistyakov 2005

uses
  Types, SysUtils;

var
  n, ns: integer;
  errorPos: integer;
  m: integer;
  i, j, k: integer;
begin
  n := StrToInt(ParamStr(1));
  errorPos := StrToInt(ParamStr(2));

  ns := n * n;
  WriteLn(n);
  for i := 0 to n - 1 do
    for j := 0 to n - 1 do begin
      for k := 0 to ns - 1 do begin
        m := (i + j * n + k) mod ns;
        if (i * n * ns + j * ns + k = errorPos) then
          m :=  (m + 1) mod ns;
        write(m + 1, ' ');
      end;
      writeln;
    end;
end.
