#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, k;
  cin >> n >> k;
  long long kk = n - k + 1;
  cout << n * (n * n - kk + 1) - (n - 1) * n / 2 * kk << endl;
  long long ans = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < k; j++) cout << ans++ << " ";
    long long m = -kk * i + n * n + 1;
    for (int j = k; j <= n; j++) cout << m++ << " ";
    puts("");
  }
}
