#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  long long n, m, i, j, k, t;
  cin >> n >> k;
  long long ans = 0;
  long long x = n * n;
  long long y = x - (n - k);
  for (i = 0; i < n; i++) {
    ans += y;
    y -= n - k + 1;
  }
  cout << ans << endl;
  m = 1;
  long long prt = x - (n - k), l = 1;
  long long pt = prt;
  for (i = 0; i < n; i++) {
    long long inc = 0;
    for (j = m; j < n + m; j++) {
      if (j < k + m - 1) {
        cout << j << " ";
        inc++;
      } else {
        cout << prt << " ";
        prt++;
      }
    }
    pt -= (n - k + 1);
    prt = pt;
    cout << endl;
    m += inc;
  }
}
