#include <bits/stdc++.h>
using namespace std;
const int N = 555;
int a[N][N];
int n, k;
int main() {
  cin >> n >> k;
  int right = n - k + 1;
  int lft = k - 1;
  for (int i = (1); i <= (right * n); i++) {
    int row = (i - 1) / right + 1;
    int col = i % right == 0 ? right : i % right;
    col = n - col + 1;
    a[row][col] = n * n - i + 1;
  }
  for (int i = (1); i <= (lft * n); i++) {
    int row = (i - 1) / lft + 1;
    row = n - row + 1;
    int col = i % lft == 0 ? lft : i % lft;
    a[row][col] = i;
  }
  int ans = 0;
  for (int i = (1); i <= (n); i++) ans += a[i][k];
  printf("%d\n", ans);
  for (int i = (1); i <= (n); i++) {
    for (int j = (1); j <= (n); j++) printf("%d ", a[i][j]);
    printf("\n");
  }
  return 0;
}
