#include <bits/stdc++.h>
using namespace std;
long long n, k;
long long res[1000][1000];
long long sum;
int main() {
  cin >> n >> k;
  long long _max = n * n - (n - k) - (n - k + 1) * (n - 1);
  long long before = 1;
  long long after;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (j == k - 1) {
        res[i][j] = _max;
        sum += _max;
        after = _max + 1;
        _max += (n - k + 1);
      }
      if (j < k - 1) {
        res[i][j] = before;
        before++;
      }
      if (j > k - 1) {
        res[i][j] = after;
        after++;
      }
    }
  }
  cout << sum << endl;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) cout << res[i][j] << " ";
    cout << endl;
  }
  return 0;
}
