#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int a[n][n];
  int ap = n * n - (n - k);
  int bp = ap;
  int uu = 1;
  int column_sum = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (j < (k - 1))
        a[i][j] = uu++;
      else {
        a[i][j] = ap++;
        if (j == (k - 1)) column_sum += a[i][j];
      }
    }
    ap = a[i][k - 1] - (n - k) - 1;
  }
  cout << column_sum << "\n";
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cout << a[i][j] << " ";
    }
    cout << "\n";
  }
}
