#include <bits/stdc++.h>
using namespace std;
int g[510][510];
int main(int argc, char *argv[]) {
  int n, k;
  cin >> n >> k;
  int cnt = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j < k; j++) g[i][j] = ++cnt;
  for (int i = 1; i <= n; i++)
    for (int j = k; j <= n; j++) g[i][j] = ++cnt;
  int res = 0;
  for (int i = 1; i <= n; i++) res += g[i][k];
  cout << res << endl;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) cout << g[i][j] << (j == n ? "\n" : " ");
  return 0;
}
