#include <bits/stdc++.h>
using namespace std;
int n, k;
int vis[510 * 510 + 1], idx = 1;
int fnvis() {
  while (vis[idx]) idx++;
  vis[idx] = 1;
  return idx;
}
int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> k;
  priority_queue<int> s;
  for (int i = 1; i <= n * n; i++) s.push(i);
  int g[n][n];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) g[i][j] = -1;
  for (int i = 0; i < n; i++) {
    priority_queue<int> pq;
    int c = n + 1 - k;
    while (c--) {
      int q = s.top();
      s.pop();
      pq.push(-q);
      vis[q] = 1;
    }
    for (int j = k - 1; j < n; j++) {
      g[i][j] = -1 * pq.top();
      pq.pop();
    }
  }
  int sum = 0;
  for (int i = 0; i < n; i++) sum += g[i][k - 1];
  cout << sum << endl;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (g[i][j] != -1)
        cout << g[i][j] << " ";
      else
        cout << fnvis() << " ";
    }
    cout << endl;
  }
  return (0);
}
