program distribute_numbers
  implicit none
  integer, parameter :: k = 12, n = k*(k-1)+1, p = k-1
  integer :: i, d, s, a(k)
  write(*,'(i0,x,i0)') n, k
  do i = 0, p
    do s = 1, p
      a(s) = i*p+s
    end do
    a(k) = n
    call output()
  end do
  do d = 0, p-1
    do s = 0, p-1
      do i = 0, p-1
        a(i+1) = i*p+1+mod(d*i+s,p)
      end do
      a(k) = p**2+1+d
      call output()
    end do
  end do
contains
  subroutine output()
    integer :: i
    write(*,'(i0)',advance='no') a(1)
    do i = 2, k
      write(*,'(x,i0)',advance='no') a(i)
    end do
    write(*,*)
  end
end program distribute_numbers