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