#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
using namespace std;
long long MOD = 1000000009;
double eps = 1e-12;
long long a[505][505];
void solve() {
  long long n, k;
  cin >> n >> k;
  long long d = n - k + 1, c = n * n - n + k;
  long long init = c - (n - 1) * d;
  long long cnt = 0;
  long long sum = (n * (init + c)) / 2;
  cout << sum << "\n";
  for (long long i = 0; i < n; i++) {
    long long tmp = init;
    for (long long j = 0; j < k - 1; j++) cout << ++cnt << " ";
    for (long long j = 0; j < n - k + 1; j++) cout << tmp++ << " ";
    init += d;
    cout << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1;
  for (int it = 1; it <= t; it++) {
    solve();
  }
  return 0;
}
