#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n, k, re = 0, num = 1;
  cin >> n >> k;
  int temp = n - k;
  num = n * n;
  for (int i = 0; i < n; i++) {
    re += num - temp;
    num = num - temp - 1;
  }
  num++;
  cout << re << endl;
  int num2 = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < k - 1; j++) {
      cout << num2 << " ";
      num2++;
    }
    for (int j = 0; j < temp + 1; j++) {
      cout << num << " ";
      num++;
    }
    cout << endl;
  }
}
