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