#include <bits/stdc++.h>
using namespace std;
int Set(int N, int pos) { return N = N | (1 << pos); }
int reset(int N, int pos) { return N = N & ~(1 << pos); }
bool check(int N, int pos) { return (bool)(N & (1 << pos)); }
void CI(int &_x) { scanf("%d", &_x); }
void CO(int &_x) { cout << _x; }
template <typename T>
void getarray(T a[], int n) {
  for (int i = 0; i < n; i++) cin >> a[i];
}
template <typename T>
void printarray(T a[], int n) {
  for (int i = 0; i < n - 1; i++) cout << a[i] << " ";
  cout << a[n - 1] << endl;
}
const double EPS = 1e-9;
const int INF = 0x7f7f7f7f;
int dr8[8] = {1, -1, 0, 0, 1, -1, -1, 1};
int dc8[8] = {0, 0, -1, 1, 1, 1, -1, -1};
int dr4[4] = {0, 0, 1, -1};
int dc4[4] = {-1, 1, 0, 0};
int kn8r[8] = {1, 2, 2, 1, -1, -2, -2, -1};
int kn8c[8] = {2, 1, -1, -2, -2, -1, 1, 2};
int grid[505][505];
int n, k;
int limit;
void proc(int row) {
  int col = n;
  while (col >= k) {
    grid[row][col] = limit;
    limit--;
    col--;
  }
}
void proc2(int row) {
  int col = k - 1;
  while (col >= 1) {
    grid[row][col] = limit;
    limit--;
    col--;
  }
}
int main() {
  cin >> n >> k;
  limit = n * n;
  for (int i = 1; i <= n; i++) {
    proc(i);
  }
  for (int i = 1; i <= n; i++) {
    proc2(i);
  }
  long long int ans = 0;
  for (int i = 1; i <= n; i++) ans += grid[i][k];
  cout << ans << "\n";
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) cout << grid[i][j] << " ";
    puts("");
  }
}
