#include <bits/stdc++.h>
using namespace std;
bool used[501 * 501];
int main() {
  int n, k, s = 0;
  cin >> n >> k;
  int hi = n * n - (n - k), d = (n - k) + 1;
  int ar[n + 1][n + 1];
  for (int i = 1; i <= n; i++) {
    ar[i][k] = hi;
    used[hi] = 1;
    int t = hi;
    s += hi;
    for (int j = k + 1; j <= n; j++) {
      ar[i][j] = ++t;
      used[ar[i][j]] = 1;
    }
    hi -= d;
  }
  int p = 1;
  for (int i = n; i > 0; i--) {
    for (int j = 1; j < k; j++) {
      while (used[p]) p++;
      ar[i][j] = p;
      used[p] = 1;
    }
  }
  cout << s << endl;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cout << ar[i][j] << ' ';
    }
    cout << endl;
  }
  return 0;
}
