#include<bits/stdc++.h>
#define f(i,s,e) for(int i=s;i<e;i++)
#define pb(x) push_back(x)
#define all(x) x.begin(),x.end()
using namespace std;
map < int, vector<int> > m1,m2;
int main(){
int n,m,ele;
cin>>n>>m;
f(i,1,n+1){
f(j,1,m+1){
cin>>ele;
m1[ele].pb(i+j);
}
}
f(i,1,n+1){
f(j,1,m+1){
cin>>ele;
m2[ele].pb(i+j);
}
}
for(auto it:m1) sort(all(it.second));
for(auto it:m2) sort(all(it.second));
cout<<endl;
for(auto it:m1){
for(auto x:it.second)
cout<<x<<" ";
cout<<endl;
}
cout<<endl;
for(auto it:m2){
for(auto x:it.second)
cout<<x<<" ";
cout<<endl;
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KI2RlZmluZSBmKGkscyxlKSBmb3IoaW50IGk9cztpPGU7aSsrKQojZGVmaW5lIHBiKHgpIHB1c2hfYmFjayh4KQojZGVmaW5lIGFsbCh4KSB4LmJlZ2luKCkseC5lbmQoKQp1c2luZyBuYW1lc3BhY2Ugc3RkOwptYXAgPCBpbnQsIHZlY3RvcjxpbnQ+ID4gbTEsbTI7CmludCBtYWluKCl7CiAgICBpbnQgbixtLGVsZTsKICAgIGNpbj4+bj4+bTsKICAgIGYoaSwxLG4rMSl7CiAgICAgICAgZihqLDEsbSsxKXsKICAgICAgICAgICAgY2luPj5lbGU7CiAgICAgICAgICAgIG0xW2VsZV0ucGIoaStqKTsKICAgICAgICB9CiAgICB9CiAgICAKICAgIGYoaSwxLG4rMSl7CiAgICAgICAgZihqLDEsbSsxKXsKICAgICAgICAgICAgY2luPj5lbGU7CiAgICAgICAgICAgIG0yW2VsZV0ucGIoaStqKTsKICAgICAgICB9CiAgICB9CiAgICAKICAgIGZvcihhdXRvIGl0Om0xKSAgICAgICAgc29ydChhbGwoaXQuc2Vjb25kKSk7CiAgICBmb3IoYXV0byBpdDptMikJCSAgIHNvcnQoYWxsKGl0LnNlY29uZCkpOwoKCWNvdXQ8PGVuZGw7CiAgICBmb3IoYXV0byBpdDptMSl7CiAgICAJZm9yKGF1dG8geDppdC5zZWNvbmQpCiAgICAJCWNvdXQ8PHg8PCIgIjsKCQljb3V0PDxlbmRsOwogICAgfQogICAgCiAgICBjb3V0PDxlbmRsOwogICAgZm9yKGF1dG8gaXQ6bTIpewogICAgCWZvcihhdXRvIHg6aXQuc2Vjb25kKQogICAgCQljb3V0PDx4PDwiICI7CgkJY291dDw8ZW5kbDsKICAgIH0KfQ==