// File: razimantv[P]DancingLinks/nqueens/nqueens.cpp
// Function: main
int main() {
  // Read N and whether the user wants the full list of solutions
  //  or just the count
  int N, getall;
  scanf("%d%d", &N, &getall);
  if (N < 1) {
    printf("Invalid board size\n");
    return 0;
  }

  // Every position of a queen is a subset. Hence R=N*N
  // Every row and every column should have exactly one queen. This gives 2*N
  //  essential constraints
  // Every diagonal should have at most one queen. This gives 4*N-2
  //  non-essential constraints
  int R = N * N, C1 = 2 * N, C2 = 4 * N - 2, C = C1 + C2;

  // Matrix to store the constraint adjacency matrix
  char *mat;
  mat = new (nothrow) char[R * C];
  if (mat == NULL) {
    printf("Memory allocation error\n");
    return 1;
  }
  memset(mat, 0, R * C);

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      // Queen position (i,j) corresponds to row# i*N+j of adjacency matrix
      int r = i * N + j;

      // Constraint of row i is column# i of adjacency matrix
      // Constraint of column i is column# N+i of adjacency matrix
      // Constraint of upward diagonal i is column# 2*N+i of adjacency matrix
      // Constraint of downward diagonal i is column# 4*N-1+i of adjacency
      // matrix
      int c1 = i, c2 = N + j, c3 = 2 * N + i + j, c4 = 5 * N - 2 + i - j;
      mat[r * C + c1] = 1;
      mat[r * C + c2] = 1;
      mat[r * C + c3] = 1;
      mat[r * C + c4] = 1;
    }
  }

  // Now just solve it with dancing links
  DancingLinks D;
  D.Create(R, C1, C2, mat);
  std::vector<std::vector<int> > all;
  std::vector<int> cur;
  D.solve(all, cur, true);

  // Number of solutions
  printf("%lu\n", all.size());

  if (getall != 0) {
    // User wants the locations of all the queens
    for (int i = 0; i < all.size(); i++) {
      printf("%d:\n", i);
      for (int j = 0; j < all[i].size(); j++) {
        printf("%d %d\n", all[i][j] / N, all[i][j] % N);
      }
    }
  }
  return 0;
}

// Assembly:
subq $0xe8, %rsp
movl $0x0, 0xe4(%rsp)
leaq 0x3d8b(%rip), %rdi      # 0x5004
leaq 0xe0(%rsp), %rsi
leaq 0xdc(%rsp), %rdx
movb $0x0, %al
callq 0x10e0
cmpl $0x1, 0xe0(%rsp)
jge 0x12b8
leaq 0x3d68(%rip), %rdi      # 0x5009
movb $0x0, %al
callq 0x1040
movl $0x0, 0xe4(%rsp)
jmp 0x1722
movl 0xe0(%rsp), %eax
imull 0xe0(%rsp), %eax
movl %eax, 0xd8(%rsp)
movl 0xe0(%rsp), %eax
shll %eax
movl %eax, 0xd4(%rsp)
movl 0xe0(%rsp), %eax
shll $0x2, %eax
subl $0x2, %eax
movl %eax, 0xd0(%rsp)
movl 0xd4(%rsp), %eax
addl 0xd0(%rsp), %eax
movl %eax, 0xcc(%rsp)
movl 0xd8(%rsp), %eax
imull 0xcc(%rsp), %eax
movslq %eax, %rdi
movq 0x6cb0(%rip), %rsi      # 0x7fd0
callq 0x1130
movq %rax, 0xc0(%rsp)
cmpq $0x0, 0xc0(%rsp)
jne 0x1356
leaq 0x3cde(%rip), %rdi      # 0x501d
movb $0x0, %al
callq 0x1040
movl $0x1, 0xe4(%rsp)
jmp 0x1722
movq 0xc0(%rsp), %rdi
movl 0xd8(%rsp), %eax
imull 0xcc(%rsp), %eax
movslq %eax, %rdx
xorl %esi, %esi
callq 0x1090
movl $0x0, 0xbc(%rsp)
movl 0xbc(%rsp), %eax
cmpl 0xe0(%rsp), %eax
jge 0x14f5
movl $0x0, 0xb8(%rsp)
movl 0xb8(%rsp), %eax
cmpl 0xe0(%rsp), %eax
jge 0x14dd
movl 0xbc(%rsp), %eax
imull 0xe0(%rsp), %eax
addl 0xb8(%rsp), %eax
movl %eax, 0xb4(%rsp)
movl 0xbc(%rsp), %eax
movl %eax, 0xb0(%rsp)
movl 0xe0(%rsp), %eax
addl 0xb8(%rsp), %eax
movl %eax, 0xac(%rsp)
movl 0xe0(%rsp), %eax
shll %eax
addl 0xbc(%rsp), %eax
addl 0xb8(%rsp), %eax
movl %eax, 0xa8(%rsp)
imull $0x5, 0xe0(%rsp), %eax
subl $0x2, %eax
addl 0xbc(%rsp), %eax
subl 0xb8(%rsp), %eax
movl %eax, 0xa4(%rsp)
movq 0xc0(%rsp), %rax
movl 0xb4(%rsp), %ecx
imull 0xcc(%rsp), %ecx
addl 0xb0(%rsp), %ecx
movslq %ecx, %rcx
movb $0x1, (%rax,%rcx)
movq 0xc0(%rsp), %rax
movl 0xb4(%rsp), %ecx
imull 0xcc(%rsp), %ecx
addl 0xac(%rsp), %ecx
movslq %ecx, %rcx
movb $0x1, (%rax,%rcx)
movq 0xc0(%rsp), %rax
movl 0xb4(%rsp), %ecx
imull 0xcc(%rsp), %ecx
addl 0xa8(%rsp), %ecx
movslq %ecx, %rcx
movb $0x1, (%rax,%rcx)
movq 0xc0(%rsp), %rax
movl 0xb4(%rsp), %ecx
imull 0xcc(%rsp), %ecx
addl 0xa4(%rsp), %ecx
movslq %ecx, %rcx
movb $0x1, (%rax,%rcx)
movl 0xb8(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xb8(%rsp)
jmp 0x13a1
jmp 0x14df
movl 0xbc(%rsp), %eax
addl $0x1, %eax
movl %eax, 0xbc(%rsp)
jmp 0x1382
leaq 0x88(%rsp), %rdi
movq %rdi, 0x38(%rsp)
callq 0x1d40
movq 0x38(%rsp), %rdi
movl 0xd8(%rsp), %esi
movl 0xd4(%rsp), %edx
movl 0xd0(%rsp), %ecx
movq 0xc0(%rsp), %r8
callq 0x1f30
jmp 0x1530
leaq 0x60(%rsp), %rdi
movq %rdi, 0x28(%rsp)
callq 0x1740
leaq 0x48(%rsp), %rdi
movq %rdi, 0x30(%rsp)
callq 0x1750
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
leaq 0x88(%rsp), %rdi
movl $0x1, %ecx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
callq 0x2820
jmp 0x1572
leaq 0x60(%rsp), %rdi
callq 0x1760
movq %rax, %rsi
leaq 0x3ab0(%rip), %rdi      # 0x5036
xorl %eax, %eax
callq 0x1040
jmp 0x158f
cmpl $0x0, 0xdc(%rsp)
je 0x16e5
movl $0x0, 0x44(%rsp)
movslq 0x44(%rsp), %rax
movq %rax, 0x20(%rsp)
leaq 0x60(%rsp), %rdi
callq 0x1760
movq %rax, %rcx
movq 0x20(%rsp), %rax
cmpq %rcx, %rax
jae 0x16e3
movl 0x44(%rsp), %esi
leaq 0x3a66(%rip), %rdi      # 0x503b
xorl %eax, %eax
callq 0x1040
jmp 0x15de
movl $0x0, 0x40(%rsp)
movslq 0x40(%rsp), %rax
movq %rax, 0x18(%rsp)
movslq 0x44(%rsp), %rsi
leaq 0x60(%rsp), %rdi
callq 0x1780
movq %rax, %rdi
callq 0x17a0
movq %rax, %rcx
movq 0x18(%rsp), %rax
cmpq %rcx, %rax
jae 0x16d1
movslq 0x44(%rsp), %rsi
leaq 0x60(%rsp), %rdi
movq %rdi, 0x8(%rsp)
callq 0x1780
movq %rax, %rdi
movslq 0x40(%rsp), %rsi
callq 0x17c0
movq 0x8(%rsp), %rdi
movl (%rax), %eax
movl 0xe0(%rsp), %ecx
cltd
idivl %ecx
movl %eax, 0x14(%rsp)
movslq 0x44(%rsp), %rsi
callq 0x1780
movq %rax, %rdi
movslq 0x40(%rsp), %rsi
callq 0x17c0
movl 0x14(%rsp), %esi
movl (%rax), %eax
movl 0xe0(%rsp), %ecx
cltd
idivl %ecx
leaq 0x39c4(%rip), %rdi      # 0x5040
xorl %eax, %eax
callq 0x1040
jmp 0x1685
jmp 0x1687
movl 0x40(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x40(%rsp)
jmp 0x15e6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x80(%rsp)
movl %eax, 0x7c(%rsp)
jmp 0x1713
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x80(%rsp)
movl %eax, 0x7c(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x17e0
leaq 0x60(%rsp), %rdi
callq 0x1840
jmp 0x1713
jmp 0x16d3
movl 0x44(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x44(%rsp)
jmp 0x15a5
jmp 0x16e5
movl $0x0, 0xe4(%rsp)
leaq 0x48(%rsp), %rdi
callq 0x17e0
leaq 0x60(%rsp), %rdi
callq 0x1840
leaq 0x88(%rsp), %rdi
callq 0x1dd0
jmp 0x1722
leaq 0x88(%rsp), %rdi
callq 0x1dd0
jmp 0x1731
movl 0xe4(%rsp), %eax
addq $0xe8, %rsp
retq
movq 0x80(%rsp), %rdi
callq 0x1140
nop