#include <bits/stdc++.h>
using namespace std;
int arr[505][505];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, k, i, j, l, a, b, c, d, e, sum = 0;
  cin >> n >> k;
  if (k == 1) {
    a = 1;
    c = 1;
    l = 1;
    while (l <= n) {
      sum += c;
      l++;
      c += n;
    }
    cout << sum << "\n";
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= n; j++) {
        cout << a << " ";
        a++;
      }
      cout << "\n";
    }
  } else {
    a = n * n;
    b = (n - k) + 1;
    c = b * n;
    d = (a - c) + 1;
    e = d;
    for (i = 1; i <= n; i++) {
      for (j = k; j <= n; j++) {
        arr[i][j] = e;
        e++;
        if (j == k) {
          sum += arr[i][j];
        }
      }
    }
    e = 1;
    for (i = 1; i <= n; i++) {
      for (j = 1; j < k; j++) {
        arr[i][j] = e;
        e++;
      }
    }
    cout << sum << "\n";
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= n; j++) {
        cout << arr[i][j] << " ";
      }
      cout << "\n";
    }
  }
  return 0;
}
