#include <bits/stdc++.h>
long long mat[510][510];
int main() {
  int n, k;
  long long prev, count = 1, c2;
  int i, j;
  long long res;
  scanf("%d %d", &n, &k);
  prev = n * (k - 1);
  c2 = prev + 1;
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      if (j < k - 1) {
        mat[i][j] = count++;
      } else {
        mat[i][j] = c2++;
        if (j == k - 1) res += c2 - 1;
      }
    }
  }
  printf("%I64d\n", res);
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      printf("%I64d ", mat[i][j]);
    }
    printf("\n");
  }
  return 0;
}
