#include <bits/stdc++.h>
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  int i, j, x;
  long long int sum = 0;
  for (i = (k - 1) * n + 1; i <= n * n; i += n - k + 1) {
    sum += i;
  }
  printf("%lld\n", sum);
  j = 1;
  x = (k - 1) * n + 1;
  int count = 0;
  for (i = 1; i <= n; i++) {
    count = 0;
    while (count < k - 1) {
      printf("%d ", j++);
      count++;
    }
    count = 0;
    while (count < n - k + 1) {
      printf("%d ", x++);
      count++;
    }
    count = 0;
    printf("\n");
  }
  return 0;
}
