#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, col;
  cin >> n >> col;
  int bigest = n * n - (n - col);
  int sum = 0;
  int k = 1;
  vector<vector<int> > mat(n, vector<int>(n, 0));
  for (int r = 0; r < n; r++) {
    sum += bigest;
    int place = 0;
    for (int j = 1; j < col; j++) mat[r][place++] = k++;
    for (int i = 0; i <= (n - col); i++) mat[r][place++] = bigest + i;
    bigest -= (n - col) + 1;
  }
  cout << sum << endl;
  for (int i = 0; i < n; i++) {
    int j;
    for (j = 0; j < n - 1; j++) {
      cout << mat[i][j] << " ";
    }
    cout << mat[i][j] << endl;
  }
}
