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