#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,m,a[100005],h=1,c= 1;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
freopen("TROCHOI.inp", "r", stdin);
freopen("TROCHOI.out", "w", stdout);
cin >> n >> m;
for(int i = 1; i <= n*m; i++)
{
cin >> a[i];
}
sort(a+1,a+n*m+1);
ll i = 1;
while(h <= n)
{
if(h&1)
{
for(int j = i; j <= i + m-1; j++)
cout << a[j] << ' ';
cout << '\n';
}
else
{
for(int j = i + m-1; j >= i; j--)
cout << a[j] << ' ';
cout << '\n';
}
i = i + m;
h++;
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBsbCBsb25nIGxvbmcKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKbGwgbixtLGFbMTAwMDA1XSxoPTEsYz0gMTsKaW50IG1haW4oKQp7CiAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKDApOyBjaW4udGllKDApOyBjb3V0LnRpZSgwKTsKICAgIGZyZW9wZW4oIlRST0NIT0kuaW5wIiwgInIiLCBzdGRpbik7CiAgICBmcmVvcGVuKCJUUk9DSE9JLm91dCIsICJ3Iiwgc3Rkb3V0KTsKICAgIGNpbiA+PiBuID4+IG07CiAgICBmb3IoaW50IGkgPSAxOyBpIDw9IG4qbTsgaSsrKQogICAgewogICAgICAgIGNpbiA+PiBhW2ldOwogICAgfQogICAgc29ydChhKzEsYStuKm0rMSk7CiAgICBsbCBpID0gMTsKICAgIHdoaWxlKGggPD0gbikKICAgIHsKICAgICAgICBpZihoJjEpCiAgICAgICAgewogICAgICAgICAgICBmb3IoaW50IGogPSBpOyBqIDw9IGkgKyBtLTE7IGorKykKICAgICAgICAgICAgICAgIGNvdXQgPDwgYVtqXSA8PCAnICc7CiAgICAgICAgICAgIGNvdXQgPDwgJ1xuJzsKICAgICAgICB9CiAgICAgICAgZWxzZQogICAgICAgIHsKICAgICAgICAgICAgZm9yKGludCBqID0gaSArIG0tMTsgaiA+PSBpOyBqLS0pCiAgICAgICAgICAgICAgICBjb3V0IDw8IGFbal0gPDwgJyAnOwogICAgICAgICAgICBjb3V0IDw8ICdcbic7CiAgICAgICAgfQogICAgICAgIGkgPSBpICsgbTsKICAgICAgICBoKys7CiAgICB9Cgp9Cg==