#include <bits/stdc++.h>
using namespace std;
long long p(long long n) {
  for (long long i = 2; i * i <= n; i++) {
    if (n % i == 0) return 0;
  }
  return 1;
}
signed main() {
  long long t = 1;
  while (t--) {
    long long n, k, s = 0;
    cin >> n >> k;
    long long f = n * n;
    long long a[n][n];
    for (long long i = 0; i < n; i++) {
      for (long long j = n - 1; j >= k - 1; j--) {
        a[i][j] = f;
        f--;
        if (j == k - 1) {
          s += a[i][j];
        }
      }
    }
    f = 1;
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < k - 1; j++) {
        a[i][j] = f;
        f++;
      }
    }
    cout << s << "\n";
    for (long long i = 0; i < n; i++) {
      for (long long j = 0; j < n; j++) {
        cout << a[i][j] << " ";
      }
      cout << "\n";
    }
  }
}
