#include <bits/stdc++.h>
using namespace std;
long long n, k;
long long ans;
long long matriz[505][505];
int main() {
  ans = 0;
  cin >> n >> k;
  long long aux = (n - k + 1) * n;
  aux = n * n - aux + 1;
  long long curr1 = 1;
  long long curr2 = aux;
  for (int i = 0; i < n; ++i) {
    int j = 0;
    for (; j < k - 1; ++j) {
      matriz[i][j] = curr1;
      curr1++;
    }
    ans += curr2;
    matriz[i][j] = curr2;
    curr2++;
    j++;
    for (; j < n; ++j) {
      matriz[i][j] = curr2;
      curr2++;
    }
  }
  cout << ans << endl;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n - 1; ++j) {
      cout << matriz[i][j] << ' ';
    }
    cout << matriz[i][n - 1] << endl;
  }
  return 0;
}
