#include <bits/stdc++.h>
int fun(const void *a, const void *b) { return (*(int *)a - *(int *)b); }
int main() {
  int n, k, i, j, index, halwa;
  long long int sum = 0;
  scanf("%d%d", &n, &k);
  int a[n + 1][n + 1], b[n * n];
  index = n * n - 1;
  for (i = 0; i <= n; i++) {
    for (j = 0; j <= n; j++) {
      a[i][j] = 0;
    }
  }
  for (i = 0; i < n * n; i++) b[i] = i + 1;
  qsort(b, (n * n), sizeof(int), fun);
  for (i = n; i >= 1; i--) {
    for (j = n; j >= k; j--) {
      a[i][j] = b[index--];
    }
  }
  halwa = 1;
  for (i = 1; i <= n; i++) {
    for (j = 1; j < k; j++) {
      a[i][j] = halwa;
      halwa++;
    }
  }
  for (i = 1; i <= n; i++) {
    sum += a[i][k];
  }
  printf("%lld\n", sum);
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      printf("%d ", a[i][j]);
    }
    printf("\n");
  }
  return 0;
}
