#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int inf = 0x3f3f3f3f;
int n, k, a[510][510];
int main() {
  while (~scanf("%d%d", &n, &k)) {
    int top = 1, ans = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j < k; j++) {
        a[i][j] = top++;
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = k; j <= n; j++) {
        if (j == k) ans += top;
        a[i][j] = top++;
      }
    }
    printf("%d\n", ans);
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        printf("%d", a[i][j]);
        if (j == n)
          putchar('\n');
        else
          putchar(' ');
      }
    }
  }
}
