#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, k;
  cin >> n >> k;
  k--;
  vector<vector<int> > table(n, vector<int>(n));
  long long sum = 0;
  int kth_fill = n * n - (n - k - 1);
  for (int i = int(0); i <= int(n - 1); i++) {
    table[i][k] = kth_fill;
    sum += table[i][k];
    kth_fill -= (n - k);
  }
  for (int i = int(0); i <= int(n - 1); i++)
    for (int j = int(k + 1); j <= int(n - 1); j++)
      table[i][j] = table[i][k] + (j - k);
  long long num = 1;
  for (int i = int(0); i <= int(n - 1); i++) {
    for (int j = int(0); j <= int(k - 1); j++) {
      table[i][j] = num;
      num++;
    }
  }
  cout << sum << '\n';
  for (int i = int(0); i <= int(n - 1); i++) {
    for (int j = int(0); j <= int(n - 1); j++) cout << table[i][j] << ' ';
    cout << "" << '\n';
  }
  return 0;
}
