#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int x, a, d;
  a = ((k - 1) * n) + 1;
  d = n - k + 1;
  x = (n) * ((2 * a) + (n - 1) * d);
  cout << x / 2 << endl;
  int b[n][n];
  int t = 1;
  for (int j = 0; j < n; j++) {
    for (int i = 0; i < k - 1; i++) {
      b[j][i] = t;
      t++;
    }
  }
  for (int j = 0; j < n; j++) {
    for (int i = k - 1; i < n; i++) {
      b[j][i] = t;
      t++;
    }
  }
  for (int j = 0; j < n; j++) {
    for (int i = 0; i < n; i++) {
      cout << b[j][i] << " ";
    }
    cout << endl;
  }
  return 0;
}
