#include <bits/stdc++.h>
using namespace std;
long long big = 1000000007ll;
long long n, m, k, q;
long long T;
long long ANS[1000][1000];
int main() {
  long long c1, c2, c3, c4, c5;
  long long x, y, z;
  long long a, b, c, d, e, f;
  std::cin >> n >> k;
  k--;
  c3 = 1;
  for (c1 = 0; c1 < n; c1++) {
    for (c2 = 0; c2 < k; c2++) {
      ANS[c1][c2] = c3;
      c3++;
    }
  }
  long long ans = 0;
  for (c1 = 0; c1 < n; c1++) {
    for (c2 = k; c2 < n; c2++) {
      if (c2 == k) {
        ans += c3;
      }
      ANS[c1][c2] = c3;
      c3++;
    }
  }
  std::cout << ans << "\n";
  for (c1 = 0; c1 < n; c1++) {
    for (c2 = 0; c2 < n; c2++) {
      std::cout << ANS[c1][c2] << " ";
    }
    std::cout << "\n";
  }
  return 0;
}
