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