import std.stdio, std.string, std.conv;
import std.typecons;
import std.algorithm;
void main(){
    auto nk = readln.chomp.split.map!(to!int);
    int n = nk[0];
    int k = nk[1];

    int l = 1;
    int r = n*n - n*(n-k+1) + 1;

    int sum = 0;
    for(int x = r; x <= n*n; x += n-k+1){
        sum += x;
    }

    writeln(sum);

    foreach(x; 0..n){
        foreach(i; 0..(k-1)){
            write(l);
            l++;
            write(" ");
        }
        foreach(i; k..(n+1)){
            write(r);
            r++;
            if(i!=n)write(" ");
        }
        writeln();
    }
}