#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  int a[n * n];
  for (int i = 1; i <= n * n; ++i) a[i - 1] = i;
  if (k == 1) {
    long long sum = 0;
    for (int i = 0; i < n; ++i) {
      sum += n * i + 1;
    }
    cout << sum << endl;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n - 1; ++j) {
        printf("%d ", a[i * n + j]);
      }
      printf("%d\n", a[i * n + n - 1]);
    }
    return 0;
  }
  vector<int> v[n];
  k--;
  for (int i = 0; i < k * n; ++i) {
    v[i % k].push_back(i + 1);
  }
  for (int i = k * n; i < n * n; ++i) {
    v[(i - k * n) % (n - k) + k].push_back(i + 1);
  }
  long long sum = 0;
  for (int i = 0; i < n; ++i) {
    sum += v[k][i];
  }
  cout << sum << endl;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n - 1; ++j) {
      printf("%d ", v[j][i]);
    }
    printf("%d\n", v[n - 1][i]);
  }
  return 0;
}
