#include <bits/stdc++.h>
using namespace std;
int main() {
  ifstream fin("input.txt");
  int i, j, n, a, b, c, ans = 0, on = 0, t, p;
  cin >> n >> a;
  b = a - 1;
  vector<vector<int> > vec(n, vector<int>(n));
  for (i = 0; i < n; i++) {
    for (j = 0; j < b; j++) {
      vec[i][j] = i * b + j + 1;
    }
  }
  t = n * b - b + 1;
  for (i = 0; i < n; i++) {
    p = t + i * (n - b);
    for (j = b; j < n; j++) {
      vec[i][j] = p + j;
    }
  }
  for (i = 0; i < n; i++) {
    on += vec[i][b];
  }
  cout << on << endl;
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      cout << vec[i][j] << " ";
    }
    cout << endl;
  }
  return 0;
}
