#include <bits/stdc++.h>
using namespace std;
int n, k, a[555][555];
int main() {
  scanf("%d %d", &n, &k);
  int c = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < k; j++) {
      a[i][j] = c++;
    }
  }
  int sum = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = k; j <= n; j++) {
      if (j == k) sum += c;
      a[i][j] = c++;
    }
  }
  printf("%d\n", sum);
  for (int i = 1; i <= n; i++) {
    printf("%d", a[i][1]);
    for (int j = 2; j <= n; j++) printf(" %d", a[i][j]);
    printf("\n");
  }
  return 0;
}
