#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
const int inf = 1000000000;
int retrix[1000][1000], n, k;
int main() {
  cin >> n >> k;
  int dis = n - k + 1;
  int bg = (k - 1) * n + 1;
  int bg2 = 1;
  int sum = 0;
  for (int i = 0; i < n; ++i) {
    retrix[i][k - 1] = bg;
    sum += bg;
    for (int j = 0; j < k - 1; ++j) {
      retrix[i][j] = bg2++;
    }
    int temp = bg;
    for (int j = k; j < n; ++j) {
      retrix[i][j] = ++temp;
    }
    bg += dis;
  }
  cout << sum << endl;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cout << retrix[i][j];
      if (j != n - 1) cout << " ";
    }
    cout << endl;
  }
  return 0;
}
